我创建了以下代码:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
Console.WriteLine("M Start");
MyMethodAsync();
Console.WriteLine("M end");
Console.Read();
}
static async Task MyMethodAsync()
{
await Task.Yield();
Task<int> longRunningTask = LongRunningOperationAsync();
Console.WriteLine("M3");
//and now we call await on the task
int result = await longRunningTask;
//use the result
Console.WriteLine(result);
}
static async Task<int> LongRunningOperationAsync()
{
await Task.Delay(1000);
return 1;
}
}
}
输出:
M Start
M end
M3
1
哪个好,但是当我查看线程分析器时它显示了这个:
然后这个:
然后这个:
所以看起来我生成线程,但是从msdn说:
使用Async和Await进行异步编程:线程async和await关键字不会导致创建其他线程 . 异步方法不需要多线程,因为异步方法不能在自己的线程上运行 . 该方法在当前同步上下文上运行,并仅在方法处于活动状态时在线程上使用时间 . 您可以使用Task.Run将CPU绑定的工作移动到后台线程,但后台线程无助于只等待结果可用的进程 .
我错过了什么或者不理解什么?谢谢 .
2 回答
我在博客上解释how async and await work with threads and contexts . 总之,当
await
需要等待异步操作完成时,它将"pause"当前async
方法并且(默认情况下)捕获"context" .异步操作完成后,"context"用于恢复
async
方法 . 这"context"是SynchronizationContext.Current
,除非它是null
,在这种情况下它是TaskScheduler.Current
. 在您的情况下,上下文最终成为线程池上下文,因此async
方法的其余部分将发送到线程池 . 如果从UI线程运行相同的代码,则上下文将是UI上下文,并且所有async
方法将在UI线程上恢复 .是 . 它将CPU绑定或I / O绑定工作从进程的线程池移动到其他线程,以便它不在UI线程或当前同步上下文上执行,它不会创建新线程,这在MSDN描述中意味着什么 .