想確認與他相關的內容ConfigureAwait
。迄今爲止,我默認閱讀Configureawait
設置爲true。但它可以設置爲false。有些人說when it's true (default) then all code after await
將運行within context
,如果設置爲false,則不會。因此,根據示例,如果我將其設置爲false,那麼DoIndependentWork
將無法寫入控件,除非我執行Dispatcher,因爲在這種情況下,它不會在UI線程上運行上下文,如果它爲true。這是對的嗎? So generally saying when false is set all after await will not be running on main thread (not within context) so for example access to the controls will be not possible (unless dispatcher of course)
。這樣對嗎?我曾經使用過它,就好像它是WIndows窗體一樣,然後上下文就是UI線程,所以簡單的說我設置false意味着它將無法訪問控件,如果它真的會起作用。這一切都在思考嗎?那麼控制檯應用程序/ windows服務怎麼樣,這是什麼上下文,它真的很重要,我設置true或false?異步/等待configureawait行爲和上下文
Async Function AccessTheWebAsync1() As Task(Of Integer)
Dim getStringTask As Task(Of String) = New HttpClient().GetStringAsync("http://msdn.microsoft.com")
Dim urlContents As String = Await getStringTask.ConfigureAwait(false)
DoIndependentWork()
Return urlContents.Length
End Function
Sub DoIndependentWork()
If ResultsTextBox.Dispatcher.CheckAccess() Then
Write()
Else
ResultsTextBox.Dispatcher.Invoke(New Action(AddressOf Write))
End If
'ResultsTextBox.Text &= "Working . . . . . . ." & vbCrLf
End Sub
why -1?我的問題是否有問題? – Dino
不知道-1它不是我,我不明白爲什麼說實話。不過值得注意的是,你有很多問題有答案,但尚未被標記爲已接受。請參考[遊覽]瞭解網站的工作原理。對於每個接受的答案,您也獲得2個代表。然後你可以看看upvoting這是一種表達感謝的方式。 – Bugs