Await 演算子 (Visual Basic)

必要なタスクが完了するまで非同期メソッドまたはラムダ式のオペランドにメソッドの実行を中断するには、Await の演算子を追加します。タスクは、進行中の作業を表します。

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())を返します。タスクは、操作が完了したときに実際のバイト配列を生成する約束です。getContentsTask が完了するまで getContentsTask に Await 演算子が SumPageSizesAsync の実行を中断するために適用されます。一方、コントロールは 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 Web サイトから 開発者コード サンプル サンプルをダウンロードできます。この例では AsyncWalkthrough_HttpClient のプロジェクトにあります。

Await が Task(Of TResult)を返すメソッドの呼び出しの結果に適用されている場合、Await の式の型が TResult です。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 の式の後で、新しい登録にコンパイラに非同期のメソッドの残りの部分を、必要なタスクの継続されます。コントロールは、非同期のメソッド呼び出し元に制御が戻ります。タスクを完了すると、継続を開始し、場所からプロセスまたは async のメソッドの実行が再開されます。

Await の式は Async 修飾子によって示されるラムダ式または、外側のメソッドの本体でのみ実行できます。用語は、コンテキスト キーワードのみの役割を待機します。他の場所では、識別子として解釈されます。非同期のメソッドまたはラムダ式内では、Await の式はクエリ式で、Try… Catch… finally ステートメントの catch または finally ブロックで、For または For Each ループのループ制御変数の式、SyncLock のステートメント本体に含めることはできません。

例外

ほとんどの非同期のメソッドは TaskTask<TResult>を返します。返されたタスクのプロパティは、非同期のメソッドによって例外が発生したり、キャンセルまたはどうか、最終的にに何かどうかタスクが完了した状態と履歴に関する情報を伝え、など)。Await の演算子は、これらのプロパティにアクセスします。

例外を発生させるタスクを返す非同期のメソッドを待機 Await、演算子での例外。

[キャンセル]返される非同期タスクのメソッドを待機 Await では、演算子の OperationCanceledException

違反状態にある単一のタスクが複数の例外を反映できます。たとえば、タスクは Task.WhenAllへの呼び出し結果になることがあります。このようなタスクで待機すると、例外の要求の操作で 1 だけ。ただし、されるであるかは例外のどの予測できません。

非同期のメソッドのエラー処理の例については、Try...Catch...Finally ステートメント (Visual Basic)を参照してください。

使用例

次の Windows フォームには、非同期のメソッドの Await、WaitAsynchronouslyAsyncの使用例です。WaitSynchronouslyの動作を持つそのメソッドの動作の違いを確認します。Await 演算子なしでは、WaitSynchronously 本体の Thread.Sleep に定義と呼び出しの Async 修飾子の使用に関係なく同期的に実行されます。

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)