Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decide When to Use ConfigureAwait(false)

Tags:

c#

async-await

If the statements in the call graph after "await" point does not access any object whose type is not derived from System.Windows.UIElement class, can we say that the developer safely use ConfigureAwait(false) for Windows Mobile apps?

What kind of statements must have been executed on UI thread besides updating GUI elements?

like image 470
Tom K. Avatar asked Sep 07 '13 06:09

Tom K.


People also ask

Which do I use ConfigureAwait True or false?

In 99% of the cases, you should use ConfigureAwait(false). In . NET Framework by default the Task execution will continue on the captured context, this is ConfigureAwait(true).

When should I use the task ConfigureAwait Boolean method?

When an asynchronous method awaits a Task directly, continuation usually occurs in the same thread that created the task, depending on the async context. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. To avoid these problems, call Task. ConfigureAwait(false) .

Why You Should Use ConfigureAwait?

ConfigureAwait(continueOnCapturedContext: false) is used to avoid forcing the callback to be invoked on the original context or scheduler.


1 Answers

Any code that directly (or indirectly) manipulates UI elements should be run in the UI context. Usually, this just includes direct manipulation and updating ViewModels.

In all other situations, you should use ConfigureAwait(false).

like image 82
Stephen Cleary Avatar answered Sep 28 '22 10:09

Stephen Cleary