← Back to C# Course | Chapter 13: Async Programming | Lesson 4 of 6

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:

  1. ConfigureAwait
Syntax
csharp
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

markup
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);
    }
}
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
  1. Using ConfigureAwait(false) in code that later touches UI controls, which fails on the wrong thread.
  2. Thinking it makes the method faster on its own.
  3. Forgetting that it only affects the await it is attached to.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.