ConfigureAwait
ConfigureAwait tells a program that after waiting it does not have to return to exactly where it started, like not needing to go back to your same seat after a break. It can make some code more efficient.
In this page:
Syntax
await SomeAsyncOperation().ConfigureAwait(false); // do not resume on the original context
ConfigureAwait
ConfigureAwait(false) tells await not to try to resume execution back on the original synchronization context (like a UI thread), which can improve performance and avoid deadlocks in library code.
In console apps and modern ASP.NET Core there's no special context to return to, so it makes less practical difference there. It's mainly a library-authoring best practice rather than something application code needs everywhere.
Note:
Use
ConfigureAwait(false) in reusable library code that has no reason to depend on a specific UI thread.Example: ConfigureAwait
using System;
using System.Threading.Tasks;
class Program
{
static async Task<int> LoadDataAsync()
{
await Task.Delay(50).ConfigureAwait(false);
return 7;
}
static async Task Main(string[] args)
{
int value = await LoadDataAsync();
Console.WriteLine(value);
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
ConfigureAwait(false)in code that later touches UI controls, which fails on the wrong thread. - Thinking it makes the method faster on its own.
- Forgetting that it only affects the
awaitit is attached to.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: