可以使用 await 关键字来调用返回任务的异步方法 GreetingAsync。使用 await 关键字需要有用 async 修饰符声明的方法。在 GreetingAsync 方法完成前,该方法内的其他代码不会继续执行。但是,启动 CallerWithAsync 方法的线程可以被重用。该线程没有被阻塞:.
private async static void CallerWithAsync()
{
TraceThreadAndTask($"started(nameof(CallerWithAsync)}");
string result = await GreetingAsync("Stephanie");
Console.WriteLine(result);
TraceThreadAndTask($"ended {nameof(CallerWithAsync)}");
}
started CallerWithAsync in thread 2 and no task
running GreetingAsync in thread 3 and task 1
running Greeting in thread 3 and task 1
Hello,Stephanie
ended CallerWithAsync in thread 3 and no task
如果异步方法的结果不传递给变量,也可以直接在参数中使用 await 关键字。在这里,GreetingAsync 方法返回的结果将像前面的代码片段一样等待,但是这一次的结果会直接传给 Console.WriteLine 方法:
private async static void CallerwithAsync2 ()
{
TraceThreadAndTask($"started {nameof(CallerWithAsync2)}");
Console.WriteLine(await GreetingAsync("Stephanie"));
TraceThreadAndTask($"ended {nameof(CallerWithAsync2)}");
}
注意
在 C# 7 中,async 修饰符可以用于返回 void 的方法,或者用于返回一个提供GetAwaiter 方法的对象。.NET 提供 Task 和 ValueTask 类型。通过 Windows 运行库,还可以使用 IAsyncOperation。应该避免给带有 void 的方法使用 async 修饰符。