12

You have the following method:

async Task DoWorkAsync();

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

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

The only one I see, is that Visual Studio gives a warning when using the first one, informing you that method execution will continue without the result being awaited.

lekroif
  • 992
  • 3
  • 12
  • 24

2 Answers2

12

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

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

Yes; they're completely different. The first one starts the asynchronous method and then continues the current method immediately. The second one (asynchronously) waits for the asynchronous method to complete.

There are two major semantic differences:

  1. When the code after this line executes. If you await DoWorkAsync, then the following code will not execute until after DoWorkAsync completes. If you just call DoWorkAsync without awaiting it, then the following code will execute as soon as DoWorkAsync yields.
  2. How exceptions are handled. If you await DoWorkAsync, then any exceptions from DoWorkAsync will propagate naturally. If you just call DoWorkAsync without awaiting it, then any exceptions will be silently captured and placed on the returned task (which is ignored, hence the compiler warning).
Community
  • 1
  • 1
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
8

ConfigureAwait(false) says "don't capture the synchronization context". This means that you are still going to await the results, but when it continues it won't try to marshall you back onto the UI thread.

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

  • If you are writing an application that is UI-bound (e.g. WPF, Silverlight, Windows 8) then you should NOT use ConfigureAwait(false) because you'll continue on the wrong thread.

  • If you are writing an application that is context sensitive (e.g. ASP.NET MVC controllers) then you should NOT use ConfigureAwait(false) because you'll continue on the wrong thread.

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

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • 2
    `ConfigureAwait(false)` doesn't capture the current `SynchronizationContext`, not `ExecutionContext`. In ASP.NET environment, the only difference is whether the call is marshaled back to the request context or not. – Yuval Itzchakov Aug 03 '14 at 06:44
  • 2
    @YuvalItzchakov, `ExecutionContext` is actually captured regardless of `ConfigureAwait` or `SynchronizationContext`: [ExecutionContext vs SynchronizationContext](http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx). – noseratio Aug 03 '14 at 11:08
  • 2
    Yes, i know, i read that. Thats why i stated the answer which says *ConfigureAwait(false) says "don't capture the execution context"* is not true, since the `ExecutionContext` is captured regardless. – Yuval Itzchakov Aug 03 '14 at 11:47
  • @YuvalItzchakov you are allowed to correct the answer directly. – Jonathan Allen Aug 04 '14 at 18:38