我知道对正在等待的任务调用 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 调用之后方法中没有任何内容?
最佳答案
is the compiler smart enough to prevent an unnecessary return to the original context?
还没有。
Will there be a performance penalty or potential deadlock possible here, even though there is nothing in the method after the await call?
是的。
关于wpf - 最后一个方法行上的 Task.ConfigureAwait(false) 有什么影响吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41898665/