C# 调用异步方法

可以使用 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)}");}
运行应用程序时,可以从第一个输出中看到没有任务。GreetingAsync 方法在一个任务中运行,这个任务使用的线程与调用者不同。然后,同步 Greeting 方法在此任务中运行。当 Greeting 方法返回时,GreetingAsync 方法返回,在等待之后,作用域返回到 CallerWithAsync 方法中。现在,CallerWithAsync 方法在不同的线程中运行。不再有任务了,但是尽管这个方法是从线程 2 开始的,但是在使用了 await 线程 3 之后。await 确保在任务完成后继续执行,但现在它使用了另一个线程。这种行为在控制台应用程序和具有同步上下文的应用程序之间是不同的:
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,Stephanieended 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 修饰符。