What are the differences between using ConfigureAwait(false) and Task.Run?

C#.Net.Net 4.5Async AwaitC# 5.0

C# Problem Overview


I understand that it's recommended to use ConfigureAwait(false) for awaits in library code so that subsequent code does not run in the caller's execution context, which could be a UI thread. I also understand that await Task.Run(CpuBoundWork) should be used instead of CpuBoundWork() for the same reason.

##Example with ConfigureAwait##

public async Task<HtmlDocument> LoadPage(Uri address)
{
	using (var client = new HttpClient())
	using (var httpResponse = await client.GetAsync(address).ConfigureAwait(false))
	using (var responseContent = httpResponse.Content)
	using (var contentStream = await responseContent.ReadAsStreamAsync().ConfigureAwait(false))
		return LoadHtmlDocument(contentStream); //CPU-bound
}

##Example with Task.Run##

public async Task<HtmlDocument> LoadPage(Uri address)
{
	using (var client = new HttpClient())
	using (var httpResponse = await client.GetAsync(address))
		return await Task.Run(async () =>
		{
			using (var responseContent = httpResponse.Content)
			using (var contentStream = await responseContent.ReadAsStreamAsync())
				return LoadHtmlDocument(contentStream); //CPU-bound
		});
}

What are the differences between these two approaches?

C# Solutions


Solution 1 - C#

When you say Task.Run, you are saying that you have some CPU work to do that may take a long time, so it should always be run on a thread pool thread.

When you say ConfigureAwait(false), you are saying that the rest of that async method does not need the original context. ConfigureAwait is more of an optimization hint; it does not always mean that the continuation is run on a thread pool thread.

Solution 2 - C#

In this case, your Task.Run version will have a bit more overhead, as the first await call (await client.GetAsync(address)) will still marshal back into the calling context, as will the results of the Task.Run call.

In the first example, on the other hand, your first Async() method is configured to not require marshaling back into the calling context, which allows the continuation to run on a background thread still. As such, there won't be any marshaling back into the caller's context.

Solution 3 - C#

Agreed @Stephen answer, If still confusion see below screenshots 1# Without ConfigureAwait(false)
See below image Main thread trying to update Label enter image description here

2# With ConfigureAwait(false)
See below image working thread trying to update label enter image description here

Solution 4 - C#

As a side note, in both cases LoadPage() could still block your UI thread, because await client.GetAsync(address) needs time to create a task to pass to ConfigureAwait(false). And your time consuming operation might have already started before task is returned.

One possible solution is to use SynchronizationContextRemover from here:

public async Task<HtmlDocument> LoadPage(Uri address)
{
    await new SynchronizationContextRemover();

    using (var client = new HttpClient())
    using (var httpResponse = await client.GetAsync(address))
    using (var responseContent = httpResponse.Content)
    using (var contentStream = await responseContent.ReadAsStreamAsync())
        return LoadHtmlDocument(contentStream); //CPU-bound
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamView Question on Stackoverflow
Solution 1 - C#Stephen ClearyView Answer on Stackoverflow
Solution 2 - C#Reed CopseyView Answer on Stackoverflow
Solution 3 - C#Pankaj RawatView Answer on Stackoverflow
Solution 4 - C#Roman GudkovView Answer on Stackoverflow