修饰 Async
符指示修改的方法或 lambda 表达式 是异步的。 此类方法称为 异步方法。
异步方法提供了一种方便的方法,无需阻止调用方线程即可执行可能长时间运行的工作。 异步方法的调用方可以恢复其工作,而无需等待异步方法完成。
注释
Visual Studio 2012 中引入了关键字 Async
和 Await
关键字。 有关异步编程的简介,请参阅 使用 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
关键字修改的方法至少包含一个 Await 表达式或语句。 该方法同步运行,直到到达第一个 Await
,此时它会挂起,直到等待的任务完成。 同时,控件将返回到方法的调用方。 如果方法不包含 Await
表达式或语句,则该方法不会挂起,并且作为同步方法执行。 编译器警告会提醒你使用不包含 Await
的任何异步方法,因为这种情况可能指示错误。 有关详细信息,请参阅 编译器错误。
关键字 Async
是一个未保留的关键字。 它是修改方法或 lambda 表达式时的关键字。 在所有其他上下文中,它被解释为标识符。
返回类型
异步方法是 Sub 过程,或者是返回类型为或Task<TResult>的Task函数过程。 该方法无法声明任何 ByRef 参数。
如果方法的 Return 语句具有 TResult 类型的作数,则指定Task(Of TResult)
异步方法的返回类型。 如果当方法完成时未返回有意义的值,则应使用 Task
。 也就是说,对方法的调用将返回一个Task
,但在完成时Task
,等待Task
该方法的任何Await
语句都不会生成结果值。
异步子例程主要用于定义需要过程的事件处理程序 Sub
。 异步子例程的调用方无法等待它,并且无法捕获该方法引发的异常。
有关详细信息和示例,请参阅异步返回类型。
示例:
以下示例演示异步事件处理程序、异步 lambda 表达式和异步方法。 有关使用这些元素的完整示例,请参阅 演练:使用 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