ConfigureAwait controls if an awaited task continues on the original context or not. It helps avoid deadlocks and improve performance.
ConfigureAwait behavior in C Sharp (C#)
await someTask.ConfigureAwait(continueOnCapturedContext);
continueOnCapturedContext is a boolean: true means resume on the original context, false means resume on any thread.
By default, ConfigureAwait(true) is used, which captures the current context.
await Task.Delay(1000).ConfigureAwait(false);
await Task.Delay(1000).ConfigureAwait(true);
var result = await SomeAsyncMethod().ConfigureAwait(false);This program shows that in console apps (no synchronization context), ConfigureAwait(false) and ConfigureAwait(true) both result in continuations on thread pool threads (different from the original main thread).
using System; using System.Threading; using System.Threading.Tasks; class Program { static async Task Main() { Console.WriteLine($"Main thread id before await: {Thread.CurrentThread.ManagedThreadId}"); await Task.Delay(500).ConfigureAwait(false); Console.WriteLine($"Thread id after await with ConfigureAwait(false): {Thread.CurrentThread.ManagedThreadId}"); await Task.Delay(500).ConfigureAwait(true); Console.WriteLine($"Thread id after await with ConfigureAwait(true): {Thread.CurrentThread.ManagedThreadId}"); } }
In UI apps, ConfigureAwait(false) helps avoid freezing the UI by not resuming on the UI thread.
In console apps, there is usually no synchronization context, so ConfigureAwait(false) has less effect.
Always use ConfigureAwait(false) in library code to avoid unexpected context dependencies.
ConfigureAwait controls if the continuation runs on the original context or not.
Use ConfigureAwait(false) to improve performance and avoid deadlocks.
Use ConfigureAwait(true) (default) when you need to resume on the original context, like UI updates.