【发布时间】:2017-01-27 16:31:22
【问题描述】:
我了解对正在等待的任务调用 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 调用之后方法中没有任何内容,这里是否会出现性能损失或潜在的死锁?
【问题讨论】:
标签: wpf multithreading async-await task-parallel-library task