To understand the async, await, and ConfigureAwait(), let’s first understand what is Context.

What is Context?

In C# it represents some state or context in which a particular code block will executed, especially when dealing with async operations. It is a very important concept to understand how async programming performs in c# applications.

How do “async” and “await” keywords work?

async and await keywords were introduced in C# 5.0. When we mark a method with the keyword “async“ it enables the use of the “await“ keyword within the method and tells the compiler to generate a context or state machine that can handle asynchronous operations.

A state machine is a compiler-generated construct that manages the execution flow of an asynchronous method.

Let’s understand the async and await execution in simple steps.

Check out this example below.

Thread pool

Output

Output

Understanding ConfigureAwait in C#

As we understand the await keyword pauses the execution until that specific operation completes and then returns the control to the original calling thread or context.

In UI applications, this means the continuation will run on the UI thread, ensuring that you can safely update the UI.

Resuming the original context isn't necessary and can be inefficient. For instance, if you're performing background operations where you don't need to update the UI, using ConfigureAwait(false) tells the compiler not to capture and continue back to the original context.

ConfigureAwait(false) is valuable for improving performance, especially in NON-UI contexts. It’s important to understand when to use and when not to use it.

There is a general rule of thumb for when to use ConfigureAwait().

Usage

await SomeBackgroundTask().ConfigureAwait(false);
await SomeBackgroundTask().ConfigureAwait(true);

Conclusion

`async` and `await` enable non-blocking, asynchronous operations in C#, allowing methods to pause and resume without creating new threads.

The `ConfigureAwait(false)` method helps avoid resuming the original synchronization context, improving performance and avoiding potential deadlocks, depending on the requirements also.

Thanks for reading, See you next, Bye!