wpf - 最后一个方法行上的 Task.ConfigureAwait(false) 有什么影响吗?

标签 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 调用之后方法中没有任何内容?

最佳答案

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/

相关文章:

c# - 访问在 MainWindow() 构造函数期间实例化的类

Python 图像处理线程

java - 结果集和语句未在 Java 中关闭的影响

c# - 您是否必须将 Task.Run 放入方法中以使其异步?

c# - 什么时候应该在 ASP.NET MVC 中使用异步 Controller ?

c# - 向WPF中的用户报告错误的简单方法?

c# - 呈现线程上发生未指定的错误。 (NotifyPartitionIsZombie)

wpf - 如何在 WPF 中创建斜面渐变边框

java - 如何将 Spring 事务传播到另一个线程?

multithreading - 为什么在这段代码中,await 不会阻塞 ui