最后一个方法行上的Task.ConfigureAwait(false)是否会影响任何内容?

时间:2017-01-27 16:31:23

标签: wpf multithreading async-await task-parallel-library task

我知道在正在等待的任务上调用ConfigureAwait(false)有时会带来性能上的好处,因为它可以防止不必要的返回原始的SynchroniZationContext。

例如:

async Task Something()
{
   // Let's say I'm on the UI context
   //...
   await AnotherTask.ConfigureAwait(false);

   // Code here is no longer running on the UI context.
   // It runs in a thread pool synchronization context (i.e. null).
}

我的问题是:如果任务调用位于方法的最后一行,并且我们跳过了ConfigureAwait(false),那么编译器是否足够智能以防止不必要地返回原始上下文?

async Task Something()
{
   // Let's say I'm on the UI context
   //...
   await AnotherTask; // Dropped -> .ConfigureAwait(false);        
}

此处是否存在性能损失潜在死锁,即使await调用后方法中没有任何内容?

1 个答案:

答案 0 :(得分:7)

  

编译器是否足够智能以防止不必要地返回原始上下文?

还没有。

  

即使在await调用之后方法中没有任何内容,这里是否会存在性能损失或潜在的死锁?