Does Task.ConfigureAwait(false) on the last method line affect anything?

社会主义新天地 提交于 2019-12-10 17:09:34

问题


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.

For example:

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).
}

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?


回答1:


is the compiler smart enough to prevent an unnecessary return to the original context?

Not yet.

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

Yes.



来源:https://stackoverflow.com/questions/41898665/does-task-configureawaitfalse-on-the-last-method-line-affect-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!