Has anyone come across a scenario for using ConfigureAwait(true)
? Since true
is the default option I cannot see when would you ever use it.
As a general rule, every piece of code that is not in a view model and/or that does not need to go back on the main thread should use ConfigureAwait false. This is simple, easy and can improve the performance of an application by freeing the UI thread for a little longer.
ConfigureAwait in Action You capture the current context before awaiting the task, leaving it to the task context, then recovering (re-entering) it back when the task completes.
NET Core you won't need to spread ConfigureAwait(false) all over your code. Almost! This is almost true, it is still recommended the utilization of ConfigureAwait(false) for libraries as a fallback if those libraries are used within a legacy framework. But for most of the cases yes, in .
ConfigureAwait(false), it's not a matter of preference. In UI contexts, you can't do that, because you may not return to the UI thread. Therefore this is a great answer as to why the default is the way it is.
This leads to the general guidance of: if you’re writing general-purpose library code, use ConfigureAwait(false). This is why, for example, you’ll see every (or almost every) await in the .NET Core runtime libraries using ConfigureAwait(false) on every await; with a few exceptions, in cases where it doesn’t it’s very likely a bug to be fixed.
I’ve heard ConfigureAwait (false) is no longer necessary in .NET Core. True? False. It’s needed when running on .NET Core for exactly the same reasons it’s needed when running on .NET Framework. Nothing’s changed in that regard. What has changed, however, is whether certain environments publish their own SynchronizationContext.
Do not use ConfigureAwait (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.
At NML we prefer to always state explicitly how we want the task continuation to occur. So even though a Task's default is ConfigureAwait (true), we still specify it as such so that we are always cognizant of what's happening "under the hood".
true to attempt to marshal the continuation back to the original context captured; otherwise, false.
It's actually more like saying that ConfigureAwait(true)
is like using .ContinueWith( t => {...}, TaskScheduler.FromCurrentSynchronizationContext())
, where ConfigureAwait(false)
is like using .ContinueWith( t => {...})
. If you pass false, then the continuation is being allowed to run on a thread-pool thread instead of pulling back to the current synchronization context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With