在异步方法或 Lambda 表达式中对操作数应用 Await 运算符可暂停执行方法,直到所等待的任务完成。 任务表示正在进行的工作。
方法,用Await使用必须具有异步修饰符。 使用 Async 修饰符定义并且通常包含一个或多个 Await 表达式的这类方法称为异步方法。
Note:
Async和Await关键字是在 Visual Studio 2012 中引入的。 有关异步编程的介绍,请参阅使用 Async 和 Await 的异步编程。
通常情况下,应用到的任务Await运算符是实现的方法调用的返回值基于任务的异步模式,即,xref:System.Threading.Tasks.Task或xref:System.Threading.Tasks.Task`1。
在以下代码中,xref:System.Net.Http.HttpClient 方法 xref:System.Net.Http.HttpClient.GetByteArrayAsync* 返回 getContentsTask,一个 Task(Of Byte())。 当操作完成时,任务约定生成一个实际字节数组。 Await 运算符应用于 getContentsTask 以在 SumPageSizesAsync 中挂起执行,直到 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
Important: 有关完整示例,请参阅演练:使用 Async 和 Await 访问 Web。 可以从 Microsoft 网站的开发者代码示例中下载示例。 该示例处于 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 表达式之后,将剩下的异步方法注册为等待任务的后续部分。 控制权随后会返回给异步方法的调用方。 任务完成时,它会调用其延续任务,异步方法的执行会在暂停的位置处恢复。
Await 表达式只出现在由 Async 修饰符标记的一个立即封闭方法体或 lambda 表达式中。 术语Await用作关键字仅在该上下文中。 在其他位置,它会解释为标识符。 在异步方法或 lambda 表达式中,Await表达式中不能发生在查询表达式中,catch或finally块尝试...Catch...最后中的循环控制变量表达式语句For或For Each循环中,或正文中SyncLock语句。
Exceptions
大多数异步方法返回 xref:System.Threading.Tasks.Task 或 xref:System.Threading.Tasks.Task`1。 返回任务的属性携带有关其状态和历史记录的信息,如任务是否完成、异步方法是否导致异常或已取消以及最终结果是什么。 Await 运算符可访问这些属性。
如果等待的任务返回异步方法导致异常,则 Await 运算符会重新引发异常。
如果等待的返回任务的异步方法取消,Await 运算符将重新引发 xref:System.OperationCanceledException。
处于故障状态的单个任务可以反映多个异常。 例如,任务可能是对 xref:System.Threading.Tasks.Task.WhenAll*?displayProperty=nameWithType 调用的结果。 等待此类任务时,等待操作仅重新引发异常之一。 但是,无法预测重新引发的异常。
异步方法中的错误处理示例,请参阅尝试...Catch...Finally 语句。
示例
下面的 Windows 窗体示例阐释如何在异步方法 WaitAsynchronouslyAsync 中使用 Await。 将该方法的行为与 WaitSynchronously 的行为进行对比。 如果没有应用 Await 运算符,WaitSynchronously 就会同步运行,而不管其定义中是否使用了 Async 修饰符和在主体中是否调用了 xref:System.Threading.Thread.Sleep*?displayProperty=nameWithType。
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
请参阅
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。
