示例:
public async Task Method1()
{
//uses ASPNetSynchronisationContext
await Method2().ConfigureAwait(false);
//uses ThreadPoolContext as we didn't capture the context above
await Method3();
//uses ThreadPoolContext
await Method4();
}
我的问题是,"等待Method3()"试图捕捉上下文?无论如何,Method4使用ThreadPool上下文。
答案 0 :(得分:0)
答案取决于Method2
是否返回完整或不完整的任务。
正如我在async
intro中所描述的那样,await
将首先检查其参数以确定它是否已经完成,如果已经完成,那么它将继续执行< EM>同步。如果未已完成,则await
将异步执行,其中包括(Task
s)捕获上下文。
因此,如果Method2
检查它时await
返回的任务尚未完成,那么await
异步执行,则在线程池上下文中恢复; Method3
和Method4
在线程池上下文中开始,并且它们的await
在线程池上下文中恢复。
但是,如果Method2
返回的任务在await
检查它时完成,那么await
同步动作,则保留在ASP.NET上下文中; Method3
和Method4
在ASP.NET上下文中开始,并在ASP.NET上下文中显示它们的await
。