To answer your questions more directly:
ConfigureAwait(true): Runs the rest of the code on the same thread the code before the await was run on.
Not necessarily the same thread, but the same synchronization context. The synchronization context can decide how to run the code. In a UI application, it will be the same thread. In ASP.NET, it may not be the same thread, but you will have the HttpContext
available, just like you did before.
ConfigureAwait(false): Runs the rest of the code on the same thread the awaited code was run on.
This is not correct. ConfigureAwait(false)
tells it that it does not need the context, so the code can be run anywhere. It could be any thread that runs it.
If the await is followed by a code that accesses the UI, the task should be appended with .ConfigureAwait(true)
. Otherwise, an InvalidOperationException will occur due to another thread accessing UI elements.
It is not correct that it "should be appended with .ConfigureAwait(true)
". ConfigureAwait(true)
is the default. So if that's what you want, you don't need to specify it.
- When does ConfigureAwait(false) improves performance, and when it doesn't?
Returning to the synchronization context might take time, because it may have to wait for something else to finish running. In reality, this rarely happens, or that waiting time is so minuscule that you'd never notice it.
- If writing for a GUI application, but the next lines doesn't access the UI elements. Should I use ConfigureAwait(false) or ConfigureAwait(true) ?
You could use ConfigureAwait(false)
, but I suggest you don't, for a few reasons:
- I doubt you would notice any performance improvement.
- It can introduce parallelism that you may not expect. If you use
ConfigureAwait(false)
, the continuation can run on any thread, so you could have problems if you're accessing non-thread-safe objects. It is not common to have these problems, but it can happen.
- You (or someone else maintaining this code) may add code that interacts with the UI later and exceptions will be thrown. Hopefully the
ConfigureAwait(false)
is easy to spot (it could be in a different method than where the exception is thrown) and you/they know what it does.
I find it's easier to not use ConfigureAwait(false)
at all (except in libraries). In the words of Stephen Toub (a Microsoft employee) in the ConfigureAwait FAQ:
When writing applications, you generally want the default behavior (which is why it is the default behavior).
Are my conclusions correct
- no. Async is not about threads. There is no direct correspondence betweenConfigureAwait
and the use of a certain thread. See e.g. stackoverflow.com/q/46094134/11683. – GSergTask.Run()
).ConfigureAwait(false)
premits resuming without capturing the synchronization context, not requires it. For some synchronization contexts (e.g. the one used in a Winforms application), "resuming on the same context" means "running on the UI thread". For other contexts it may not mean that, or may have no observable effect at all. – GSergConfigureAwait(false)
for "context-aware" code, like something that must execute on the UI thread after completion. Another example is accessing HttpContext in an ASP.NET WebForms application. The link I posted points to a WPF application that explores these things, including how you can still deadlock when using ConfigureAwait(false) with sloppy library code. – Crowcoder