本文介绍了为什么总是建议在每条带有await的行上编写ConfigureAwait(false),我真的需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题不在于ConfigureAwait做什么.而是为什么在所有地方我都会看到类似的东西

The question is not about what ConfigureAwait does. But rather why literally everywhere I see something like

即他们建议我写

await Method1().ConfigureAwait(false);
await Method2().ConfigureAwait(false);
// Do something else
// ...
await Method3().ConfigureAwait(false);
await Method4().ConfigureAwait(false);

但是在这种情况下,仅仅像一开始就重置上下文就不会很清楚

But in such case wouldn't be clearer just resetting context in the very beginning just once like

await Task.Yield().ConfigureAwait(false);

它保证下面的代码将在没有同步上下文的情况下执行,不是吗?

It guarantees that the code below will be executed with no sync context, doesn't it?

即我读到,如果该方法立即返回,则一次编写ConfigureAwait可能不起作用.对我来说,显而易见的解决方案就像对肯定不会立即返回的东西调用ConfigureAwait(false),哪个Task.Yield是,对吧?

I.e. I read that writing ConfigureAwait once might not work if the method returns immediately.And for me the obvious solution looks like call ConfigureAwait(false) on something that for sure doesn't return immediately, which Task.Yield is, right?

据我所知Task.Yield不再包含ConfigureAwait(不知道为什么,因为我以前知道它曾经拥有过),但是看一下Task.Yield代码,编写您的代码很容易自己的方法,除了使用空的同步上下文调用延续之外,其他操作无济于事.

Also as I know the Task.Yield doesn't contain ConfigureAwait anymore(don't know why, as I know it used to have it before), but looking at the Task.Yield code it is pretty easy to write your own method which would do nothing more but calling the continuation with an empty sync context.

对我来说,读一次尤其是写一次似乎容易得多

And for me it seems much much easier to read and especially to write when you write one time

await TaskUtility.ResetSyncContext();

比在每一行上编写ConfigureAwait都要好.

than writing ConfigureAwait on every line.

该功能(Task.Yield().ConfigureAwait(false)或类似的自定义方法)还是会丢失某些东西?

Will that work(Task.Yield().ConfigureAwait(false) or similar custom method) or I miss something?

推荐答案

我经常在Stack Overflow上看到该建议,这甚至是Stephen Cleary(Microsoft MVP)在他的异步和等待文章:

I've seen that advice often here on Stack Overflow, and it's even what Stephen Cleary (a Microsoft MVP) says in his Async and Await article:

Stephen绝对了解他的知识,我同意该建议在技术上是准确的,但是我一直认为这是一个糟糕的建议,原因有两个:

Stephen definitely knows his stuff, and I agree that the advice is technically accurate, but I've always thought that this is bad advice for two reasons:

  1. 初学者和
  2. 维护风险

首先,这对初学者来说是个坏建议,因为同步上下文是一个复杂的主题.如果开始学习 async / await ,方法是告知每个 await 应该使用" ConfigureAwait(false)">,除非该方法需要其上下文",但是您甚至不知道什么是上下文".是需要它"的含义以及什么意思,那么您不知道何时不应该使用它,因此最终总是 使用它.这意味着,除非您碰巧知道,实际上您确实需要上下文",否则您将遇到很难发现的错误.东西和这个神奇的"ConfigureAwait"事情使你失去了它.您可能要花几个小时才能弄清楚这一点.

First, it's bad advice for beginners because synchronization context is a complex subject. If you start learning async/await by being told that "ConfigureAwait(false) should be used for every await unless the method needs its context", but you don't even know what "context" is and what it means to "need it", then you don't know when you shouldn't use it, so you end up always using it. That means you can run into bugs that will be very difficult to figure out unless you happen to learn that, yes, you did actually need that "context" thing and this magical "ConfigureAwait" thing made you lose it. You can lose hours trying to figure that out.

对于任何类型的应用程序,我相信建议确实应该相反:根本不要使用 ConfigureAwait ,除非您知道它的作用并且已经确定绝对不使用在该行之后需要上下文.

For applications of any kind, I believe the advice really should be the opposite: Don't use ConfigureAwait at all, unless you know what it does and you have determined that you absolutely don't need the context after that line.

但是,根据之后要调用的方法,确定您是否不需要上下文可以很简单,也可以很复杂.但是即使那样-这也是我不同意该建议的第二个原因-只是因为您现在不需要 行之后的上下文,并不意味着以后不会添加某些代码将使用上下文.您必须希望进行更改的人知道 ConfigureAwait(false)会做什么,看到并删除它.在任何地方使用 ConfigureAwait(false)会带来维护风险.

However, determining you don't need the context can be either simple, or quite complex depending on what methods are called after. But even then - and this is the second reason I disagree with that advice - just because you don't need the context after that line right now, doesn't mean some code won't be added later that will use the context. You'll have to hope that whoever makes that change knows what ConfigureAwait(false) does, sees it, and removes it. Using ConfigureAwait(false) everywhere creates a maintenance risk.

这是另一位Stephen,Stephen Toub(Microsoft员工)在 ConfigureAwait中建议的内容FAQ 在何时应使用ConfigureAwait(false)?"标题下:

This is what another Stephen, Stephen Toub (a Microsoft employee), recommends in the ConfigureAwait FAQ under the subheading "When should I use ConfigureAwait(false)?":

在我自己的应用程序代码中,我不必费心尝试找出可以使用和不能使用它的地方.我只是忽略 ConfigureAwait 的存在.当然,只要可以使用可以改善性能,但是我真的怀疑这对任何人来说都不会有明显的区别,即使可以通过计时器进行测量也是如此.我不认为投资回报率是正数.

In my own application code, I don't bother trying to figure out where I can and can't use it. I just ignore that ConfigureAwait exists. Sure, there can be a performance improvement by using it where you can, but I really doubt that it will be a noticeable difference to any human, even if it is measurable by a timer. I don't believe the return on investment is positive.

唯一的例外是在编写库时,正如Stephen Toub在他的文章中指出的那样:

The only exception to this is when you're writing libraries, as Stephen Toub points out in his article:

这有两个原因:

  1. 一个库不知道正在使用它的应用程序的上下文,因此它无论如何都不能使用上下文,并且
  2. 如果使用该库的人决定同步等待您的异步库代码,则可能导致死锁,使他们无法更改,因为他们无法更改您的代码.(理想情况下,他们不应该这样做,但是可能会发生)

要解决您的问题的另一点:在第一个 await 上使用 ConfigureAwait(false)并不总是足够的,而在其余的代码上则不够.在库代码中的 await 上使用它.Stephen Toub在标题在我的方法中仅在第一次等待而不是在其余等待中使用ConfigureAwait(false)可以吗?"的文章.说,部分:

To address another point in your question: it's not always enough to use ConfigureAwait(false) on the first await and not the rest. Use it on every await in your library code. Stephen Toub's article under the heading "Is it ok to use ConfigureAwait(false) only on the first await in my method and not on the rest?" says, in part:

这篇关于为什么总是建议在每条带有await的行上编写ConfigureAwait(false),我真的需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:23