Trying to understand when I should use ConfigureAwait().
According to book:
When an
asyncmethod resumes after an await, by default it will resume executing within the same context. This can cause performance problems if that context was a UI context and a large number of async methods are resuming on the UI context.
Solution
To avoid resuming on a context, await the result of ConfigureAwait() and pass false for its continueOnCapturedContext parameter:
async Task ResumeWithoutContextAsync()
{
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
//This method discards its context when it resumes.
}
What is context and how to see ConfigureAwait() changes things in sample application:
static async Task ResumeWithoutContextAsync()
{
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(true);
Console.WriteLine("ManagedThreadId {0}", Thread.CurrentThread.ManagedThreadId);
//This method discards its context when it resumes.
}
static void Main(string[] args)
{
ResumeWithoutContextAsync();
Console.ReadLine();
}
I was thinking that context is Thread, but it is not.