Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use ConfigureAwait(true)?

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.

like image 734
NeddySpaghetti Avatar asked Jan 02 '14 07:01

NeddySpaghetti


People also ask

When should you use ConfigureAwait false?

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.

What is ConfigureAwait () used for?

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.

Is ConfigureAwait needed in .NET Core?

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 .

Is ConfigureAwait true default?

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.

When should I use configureawait?

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.

Is configureawait (false) no longer necessary in core?

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?

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.

What is configureawait (true) in NML?

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".


Video Answer


1 Answers

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.

like image 189
Yanshof Avatar answered Sep 20 '22 20:09

Yanshof