共用方式為


Await 運算子 (Visual Basic)

您應使用 Await 運算子會在非同步方法或 Lambda 運算式的運算元逾時方法的執行,直到等候的工作完成。 工作代表持續性的工作。

Await 所使用的方法必須有 Async 修飾詞。 這種方法,來定義 Async 修飾詞和通常包含一或多個 Await 運算式,稱為 非同步方法。

注意事項注意事項

Async 與 Await 關鍵字在 Visual Studio 2012 中引入。如需在該版本的新功能的詳細資訊,請參閱 Visual Studio 2012 的新功能

如需非同步程式設計的簡介,請參閱 使用 Async 和 Await 設計非同步程式 (C# 和 Visual Basic)

通常,您的工作套用 Await 運算子是從呼叫的傳回值為實作 以工作的非同步模式,也就是, TaskTask<TResult>的方法。

在下列程式碼中, HttpClient 方法 GetByteArrayAsync 傳回 getContentsTask,否則為 Task(Of Byte())。 當作業完成時,工作是承諾產生實際位元組陣列。 Await 運算子在 SumPageSizesAsync 套用至 getContentsTask 暫停執行,直到 getContentsTask 完成。 同時,控制權回到 SumPageSizesAsync的呼叫端。 當 getContentsTask 完成時,對位元組陣列中的 Await 運算式評估。

Private Async Function SumPageSizesAsync() As Task

    ' To use the HttpClient type in desktop apps, you must include a using directive and add a 
    ' reference for the System.Net.Http namespace.
    Dim client As HttpClient = New HttpClient() 
    ' . . . 
    Dim getContentsTask As Task(Of Byte()) = client.GetByteArrayAsync(url)
    Dim urlContents As Byte() = Await getContentsTask

    ' Equivalently, now that you see how it works, you can write the same thing in a single line.
    'Dim urlContents As Byte() = Await client.GetByteArrayAsync(url)
    ' . . .
End Function

重要

如需完整的範例,請參閱 逐步解說:使用 Async 和 Await 存取 Web (C# 和 Visual Basic)。您可以從 開發人員程式碼範例 Microsoft 網站上的範例。這個範例會 AsyncWalkthrough_HttpClient 專案。

如果 Await 套用至傳回 Task(Of TResult)方法呼叫的結果, Await 運算式的型別為參數。 如果 Await 套用至傳回 Task方法呼叫的結果, Await 運算式不會傳回值。 下列範例說明差異。

' Await used with a method that returns a Task(Of TResult).
Dim result As TResult = Await AsyncMethodThatReturnsTaskTResult()

' Await used with a method that returns a Task.
Await AsyncMethodThatReturnsTask()

Await 運算式或陳述式不會封鎖其執行的執行緒。 相反地,它會讓編譯器在 Await 運算式之後會以參與非同步方法的其餘部分,,,在等候的工作的接續。 控制項回到非同步方法的呼叫端。 當工作完成時,它會叫用其接續,,且可執行從先前暫停的地方。

Await 運算式在 Async 修飾詞表示立即封入方法或 Lambda 運算式的主體可能只會發生。 這個詞彙 等候 為只關鍵字在該內容。 在其他位置,則會將它解譯為識別項。 在非同步方法或 Lambda 運算式中, Await 運算式不能出現在查詢運算式中,在 Try…Catch…Finally 陳述式中的 catch 或 finally 區塊,在 For 或 For Each 迴圈中的迴圈控制變數運算式,或在 SyncLock 陳述式的主體。

例外狀況

大部分的非同步方法傳回 TaskTask<TResult>。 傳回的工作屬性傳用相對於其狀態和記錄的資訊,例如工作是否完成,非同步方法是否會造成例外狀況或被取消,,以及最後結果是。 Await 運算子存取這些屬性。

如果等候造成例外狀況的工作傳回的非同步方法, Await 運算子會重新擲回例外狀況。

如果您已取消的工作傳回的非同步方法, Await 運算子擲回 OperationCanceledException

在錯誤狀態的單一工作可以反映多個例外狀況。 例如,工作可能是呼叫 Task.WhenAll。 當您等候這類工作,例外狀況的等候作業擲回只有一個。 不過,您無法預測的例外狀況。

以在非同步方法的錯誤處理的範例,請參閱 Try...Catch...Finally 陳述式 (Visual Basic)

範例

下列 Windows Form 範例示範在非同步方法的 Await ,否則為 WaitAsynchronouslyAsync。 比對該方法的行為和 WaitSynchronously行為。 沒有 Await 運算子,因為 WaitSynchronously 在其定義與呼叫 Async 修飾詞同步執行到主體的 Thread.Sleep

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Call the method that runs asynchronously.
    Dim result As String = Await WaitAsynchronouslyAsync()

    ' Call the method that runs synchronously.
    'Dim result As String = Await WaitSynchronously()

    ' Display the result.
    TextBox1.Text &= result
End Sub

' The following method runs asynchronously. The UI thread is not
' blocked during the delay. You can move or resize the Form1 window 
' while Task.Delay is running.
Public Async Function WaitAsynchronouslyAsync() As Task(Of String)
    Await Task.Delay(10000)
    Return "Finished"
End Function

' The following method runs synchronously, despite the use of Async.
' You cannot move or resize the Form1 window while Thread.Sleep
' is running because the UI thread is blocked.
Public Async Function WaitSynchronously() As Task(Of String)
    ' Import System.Threading for the Sleep method.
    Thread.Sleep(10000)
    Return "Finished"
End Function

請參閱

工作

逐步解說:使用 Async 和 Await 存取 Web (C# 和 Visual Basic)

參考

Async (Visual Basic)

概念

使用 Async 和 Await 設計非同步程式 (C# 和 Visual Basic)