Difference between ConfigureAwait (false) and lack of wait?

You have the following method:

async Task DoWorkAsync(); 

Is there a difference in functionality between the following two calls:

 1. DoWorkAsync(); 2. await DoWorkAsync().ConfigureAwait(false); 

The only thing I see is that Visual Studio gives a warning on first use, informing you that the method will continue to run without the expected result.

+7
c # async-await
source share
2 answers

ConfigureAwait (false) says: "Do not commit the synchronization context." This means that you will still be waiting for the results, but when it continues, it will not try to redirect you back to the UI thread.

  • If you are writing a library for other uses, always use ConfigureAwait (false), or you may call deadlocks.

  • If you are writing an application that is associated with a UI (e.g. WPF, Silverlight, Windows 8), you should NOT use ConfigureAwait (false) because you will continue to work in the wrong thread.

    / li>
  • If you are writing a context sensitive application (e.g. ASP.NET MVC controllers), you should not use ConfigureAwait (false) because you will continue the wrong thread.

link: http://www.infoq.com/articles/Async-API-Design

+7
source share

Is there a difference in functionality between the following two calls:

  • DoWorkAsync ();
  • waiting for DoWorkAsync (). ConfigureAwait (false);

Yes; they are completely different. The first starts the asynchronous method, and then immediately continues the current method. The second (asynchronously) waits for the completion of the asynchronous method.

There are two main semantic differences:

  • When the code after this line is executed. If you are await DoWorkAsync , then the following code will not be executed until DoWorkAsync completes. If you simply call DoWorkAsync without waiting for it, the following code will be executed as soon as DoWorkAsync gives.
  • How exceptions are handled. If you are await DoWorkAsync , then any exceptions to DoWorkAsync will naturally propagate. If you simply call DoWorkAsync without waiting for it, then any exceptions will be discreetly displayed and placed in the returned task (which is ignored, therefore a warning to the compiler).
+8
source share

All Articles