Async
修飾子は、変更するメソッドまたはラムダ式が非同期であることを示します。 このようなメソッドは非同期 メソッドと呼ばれます。
非同期メソッドは、呼び出し元のスレッドをブロックすることなく、実行時間の長い可能性のある作業を行う便利な方法を提供します。 非同期メソッドの呼び出し元は、非同期メソッドの完了を待たずに作業を再開できます。
注
Async
キーワードとAwait
キーワードは、Visual Studio 2012 で導入されました。 非同期プログラミングの概要については、「Async と Await を使用した非同期プログラミング」を参照してください。
次の例は、非同期メソッドの構造を示しています。 慣例により、非同期メソッド名は "Async" で終わっています。
Public Async Function ExampleMethodAsync() As Task(Of Integer)
' . . .
' At the Await expression, execution in this method is suspended and,
' if AwaitedProcessAsync has not already finished, control returns
' to the caller of ExampleMethodAsync. When the awaited task is
' completed, this method resumes execution.
Dim exampleInt As Integer = Await AwaitedProcessAsync()
' . . .
' The return statement completes the task. Any method that is
' awaiting ExampleMethodAsync can now get the integer result.
Return exampleInt
End Function
通常、 Async
キーワードによって変更されたメソッドには、少なくとも 1 つの Await 式またはステートメントが含まれます。 メソッドは、最初の Await
に達するまで同期的に実行され、その時点で待機中のタスクが完了するまで中断されます。 その間、メソッドの呼び出し元に制御が返されます。 メソッドに Await
式またはステートメントが含まれていない場合、メソッドは中断されず、同期メソッドとして実行されます。 コンパイラ警告は、 Await
を含まない非同期メソッドに警告します。これは、その状況がエラーを示している可能性があるためです。 詳細については、 コンパイラ エラーを参照してください。
Async
キーワードは予約されていないキーワードです。 メソッドまたはラムダ式を変更するときのキーワードです。 他のすべてのコンテキストでは、識別子として解釈されます。
戻り値の型
非同期メソッドは、Sub プロシージャ、または戻り値の型がTaskまたはTask<TResult>の Function プロシージャです。 このメソッドは ByRef パラメーターを宣言できません。
メソッドの Return ステートメントに TResult 型のオペランドがある場合は、非同期メソッドの戻り値の型にTask(Of TResult)
を指定します。 メソッドの完了時に意味のある値を返さない場合は、Task
を使用します。 つまり、メソッドの呼び出しはTask
を返しますが、Task
が完了すると、Task
を待機しているAwait
ステートメントは結果値を生成しません。
非同期サブルーチンは、主に、 Sub
プロシージャが必要なイベント ハンドラーを定義するために使用されます。 非同期サブルーチンの呼び出し元は、このサブルーチンを待機できないので、メソッドがスローする例外をキャッチできません。
使用例を含む詳細については、「非同期の戻り値の型」をご覧ください。
例
次の例は、非同期イベント ハンドラー、非同期ラムダ式、および非同期メソッドを示しています。 これらの要素を使用する完全な例については、「 チュートリアル: Async と Await を使用した Web へのアクセス」を参照してください。 .NET サンプル ブラウザーからサンプルをダウンロードできます。 コード例は 、SerialAsyncExample プロジェクトにあります。
' An event handler must be a Sub procedure.
Async Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
textBox1.Clear()
' SumPageSizesAsync is a method that returns a Task.
Await SumPageSizesAsync()
textBox1.Text = vbCrLf & "Control returned to button1_Click."
End Sub
' The following async lambda expression creates an equivalent anonymous
' event handler.
AddHandler button1.Click, Async Sub(sender, e)
textBox1.Clear()
' SumPageSizesAsync is a method that returns a Task.
Await SumPageSizesAsync()
textBox1.Text = vbCrLf & "Control returned to button1_Click."
End Sub
' The following async method returns a Task(Of T).
' A typical call awaits the Byte array result:
' Dim result As Byte() = Await GetURLContents("https://msdn.com")
Private Async Function GetURLContentsAsync(url As String) As Task(Of Byte())
' The downloaded resource ends up in the variable named content.
Dim content = New MemoryStream()
' Initialize an HttpWebRequest for the current URL.
Dim webReq = CType(WebRequest.Create(url), HttpWebRequest)
' Send the request to the Internet resource and wait for
' the response.
Using response As WebResponse = Await webReq.GetResponseAsync()
' Get the data stream that is associated with the specified URL.
Using responseStream As Stream = response.GetResponseStream()
' Read the bytes in responseStream and copy them to content.
' CopyToAsync returns a Task, not a Task<T>.
Await responseStream.CopyToAsync(content)
End Using
End Using
' Return the result as a byte array.
Return content.ToArray()
End Function
こちらも参照ください
.NET