最后方法行上的Task.ConfigureAwait(false)是否会影响任何事情? [英] Does Task.ConfigureAwait(false) on the last method line affect anything?

查看:178
本文介绍了最后方法行上的Task.ConfigureAwait(false)是否会影响任何事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I understand that calling ConfigureAwait(false) on a task that is being awaited will sometimes have a performance benefit, as it prevents an unnecessary return to the original 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),那么编译器是否足够聪明,可以防止不必要的返回到原始上下文?

My question is this: If the task call is on the last line of the method, and we skip the ConfigureAwait(false), is the compiler smart enough to prevent an unnecessary return to the original context?

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

即使等待调用后方法中没有任何内容,这里是否可能会出现性能损失潜在死锁?

Will there be a performance penalty or potential deadlock possible here, even though there is nothing in the method after the await call?

推荐答案

编译器是否足够聪明,可以防止不必要的返回原始上下文?

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?

是的

这篇关于最后方法行上的Task.ConfigureAwait(false)是否会影响任何事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆