C# ConfigureAwait: When and Why to Use It
Understand what C# ConfigureAwait does, when to use ConfigureAwait(false) in async libraries, and why UI code should keep the default await behavior to avoid context-capture pitfalls.

ConfigureAwait in C# controls whether the code after an await tries to resume on the original SynchronizationContext or TaskScheduler. That decision matters because library code can cause deadlocks when it captures a UI thread's context, while UI code often needs that capture to update controls safely.
When you write await in C#, the compiler generates a continuation that runs after the awaited task completes. By default, that continuation is posted back to the original SynchronizationContext or TaskScheduler if one is present. In a UI application, that means the continuation runs on the UI thread. In a library method, that can cause deadlocks and unnecessary overhead. The ConfigureAwait method gives you control over this behavior.
What ConfigureAwait Actually Does
The ConfigureAwait method is called on an awaitable object, typically a Task or Task<T>. It takes a single bool parameter: continueOnCapturedContext. When you write:
await task.ConfigureAwait(false);You are telling the runtime that the continuation does not need to run on the original synchronization context. When continueOnCapturedContext is true (the default when you use await task), the runtime attempts to capture the current context and schedule the continuation on it. When it is false, the runtime does not try to return to a captured context; the continuation follows the default scheduling rules instead, usually running on the thread pool and sometimes running inline if the awaited task is already complete.
The key point is that ConfigureAwait only affects the context used for the continuation. It does not change the execution of the awaited task itself, nor does it change which thread the task runs on. The task runs as it normally would; only the code after the await is affected.
Why Capturing the Context Can Cause Deadlocks
The most common problem with the default behavior appears when you block on an async method from a UI thread. Consider this example:
public static async Task<string> GetDataAsync()
{
await Task.Delay(1000); // Simulate network call
return "data";
}
// In a button click handler (UI thread):
string result = GetDataAsync().Result; // Blocks the UI threadHere, GetDataAsync awaits Task.Delay. The continuation after Task.Delay tries to return to the captured SynchronizationContext (the UI thread). But the UI thread is blocked on .Result, so it cannot process the continuation. The async method waits for the UI thread, and the UI thread waits for the async method—a classic deadlock.
Using ConfigureAwait(false) inside GetDataAsync solves this:
public static async Task<string> GetDataAsync()
{
await Task.Delay(1000).ConfigureAwait(false);
return "data";
}Now the continuation runs on the thread pool, not the UI thread, so the UI thread is free to complete the .Result call. This is the primary reason library authors use ConfigureAwait(false).
When to Use ConfigureAwait(false) in Library Code
Library code—methods that are not directly tied to a UI or ASP.NET request context—should almost always use ConfigureAwait(false) for every await. The reasoning is straightforward:
- The library does not need to resume on a specific
SynchronizationContext. - Capturing the context adds overhead and can cause deadlocks if the caller blocks on the task.
- The library should be context-agnostic so it works correctly in any environment.
The main exception is a method that intentionally must return to a caller-supplied context; in ordinary I/O or domain libraries, that is rare and usually better handled at the caller boundary.
For example, a data access method that loads records from a database should not assume it is called from a UI thread:
public async Task<List<Customer>> GetCustomersAsync()
{
using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync().ConfigureAwait(false);
using var command = new SqlCommand("SELECT * FROM Customers", connection);
using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
// ...
}By adding ConfigureAwait(false) to each await, you ensure that the method never captures the caller's context. This makes the method safe to call from any thread, including a UI thread that blocks on the result.
When to Avoid ConfigureAwait(false) in UI Code
In UI applications (WPF, WinForms, Xamarin, MAUI), the SynchronizationContext is tied to the UI thread. When you await in an event handler or a method that updates UI elements, you want the continuation to run on the UI thread so you can safely modify controls. Using ConfigureAwait(false) in these cases would skip the UI context, so the continuation would typically run on a thread pool thread and could throw when trying to access UI elements.
private async void Button_Click(object sender, EventArgs e)
{
var data = await LoadDataAsync(); // Default: captures UI context
textBox.Text = data; // Safe because we're on UI thread
}If you changed that to await LoadDataAsync().ConfigureAwait(false), the continuation would no longer be scheduled through the captured UI context. In the common case it would run on a thread pool thread, and textBox.Text = data would throw an InvalidOperationException because the control is bound to a different thread. So in UI code, keep the default behavior.
ASP.NET Core is a special case: it does not have a SynchronizationContext by default, so ConfigureAwait(false) is technically unnecessary. However, using it in library code is still good practice for consistency and for scenarios where the library might be used in a different host.
Performance and Overhead Implications
Capturing the synchronization context is not free. When an await captures a context, the continuation is scheduled through that context instead of being scheduled directly. In hosts that install a custom SynchronizationContext, this overhead can add up when many awaits are chained. ConfigureAwait(false) avoids capturing that context and lets the continuation follow the default scheduling rules.
The actual cost depends on the runtime version. In .NET Framework, context capture was relatively expensive. In .NET Core and later .NET versions, the runtime was optimized and the difference is smaller. But in library code that is called frequently, avoiding unnecessary context capture is still a reasonable micro-optimization.
ConfigureAwait(false) does not by itself determine which thread the continuation runs on. It only means the continuation is not routed through a captured context. If you need to run on a specific scheduler, use Task.Run or a custom TaskScheduler instead.
Common Misconceptions and Pitfalls
One common misconception is that ConfigureAwait(false) makes the entire method run on a thread pool thread. That is not true. The awaited task itself runs wherever it is scheduled; ConfigureAwait only affects the continuation. For example, if you call a method that returns an already-completed task, the continuation may run synchronously, and ConfigureAwait(false) has no effect.
Another pitfall is using ConfigureAwait(false) in a UI method and then trying to access UI elements. As explained above, that will fail. The rule is simple: use false in library code, use the default (or true) in UI code.
ConfigureAwait is an instance method on Task and ValueTask, not an extension method. It is defined in the System.Threading.Tasks namespace, so the standard usings for async code are enough.
Finally, ConfigureAwait(false) does not prevent all deadlocks. If you have a chain of awaits and one method in the chain uses .Result or .Wait() without ConfigureAwait(false), the deadlock can still occur. The pattern must be applied consistently throughout the library.
ConfigureAwait in .NET Core and .NET 5+
In .NET Core and later .NET versions, the default behavior for await is still to capture the synchronization context if one exists. However, the runtime has been optimized to make context capture cheaper than it was in .NET Framework. Additionally, ASP.NET Core does not install a SynchronizationContext by default, so in a typical web application there is nothing to capture and ConfigureAwait(false) is redundant.
That does not mean library code should stop using it. A library can be used from a .NET Framework app, a UI app, or another host that installs a context. Using ConfigureAwait(false) keeps the library context-agnostic and avoids deadlocks if a caller blocks on the returned task.
There is also an implementation detail worth knowing: the awaiter can continue synchronously when the awaited task is already complete, so inline execution is possible in either case. That is safe and can avoid a thread-pool transition, but you should not write code that depends on it.
Best Practices for Using ConfigureAwait
A practical rule of thumb: if you are writing a method that is not a UI event handler and does not need to resume on a specific context, use ConfigureAwait(false) for every await. This includes:
- Class library methods
- Repository methods
- Service methods that are called from controllers or UI code
- Helper methods that perform I/O
In UI code, keep the default behavior so that continuations run on the UI thread. In ASP.NET Core controller actions, there is no captured context, so ConfigureAwait(false) is usually unnecessary; it does not change behavior in a typical request, and using it in application code is optional.
When you are writing a method that is intended to be used by both UI and non-UI callers, the safest choice is to use ConfigureAwait(false) internally and let the caller handle context if needed. This prevents deadlocks and keeps the library flexible.
ValueTask has a ConfigureAwait method too, so the same reasoning applies whenever you await a ValueTask or ValueTask<T>: decide whether the continuation needs the original context before choosing the setting.
By applying ConfigureAwait deliberately, you make your async code more robust, avoid common deadlock scenarios, and reduce unnecessary overhead in performance-sensitive paths.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.