---
title: "ConfigureAwait in Task Parallel Library (TPL) - Muhammad Rehan Saeed"
description: "The importance of using ConfigureAwait when using the Task Parallel Library (TPL) to improve performance and reduce context switching."
canonical_url: "https://rehansaeed.com/configureawait-task-parallel-library/"
last_updated: "2014-02-07T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: ".NET,C#,ConfigureAwait,HttpClient,Task Parallel Library,ThreadPool,Threads,TPL"
  "og:description": "The importance of using ConfigureAwait when using the Task Parallel Library (TPL) to improve performance and reduce context switching."
  "og:image": "https://rehansaeed.com/_og/s/o_e09945e4aba5876c.png"
  "og:image:alt": "ConfigureAwait in Task Parallel Library (TPL) - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "ConfigureAwait in Task Parallel Library (TPL)"
  "twitter:image": "https://rehansaeed.com/_og/s/o_e09945e4aba5876c.png"
  "twitter:image:alt": "ConfigureAwait in Task Parallel Library (TPL) - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [ConfigureAwait in Task Parallel Library (TPL)](https://rehansaeed.com/configureawait-task-parallel-library/)

Posted 7 February 2014 - **2 min read**

![Microsoft .NET](https://rehansaeed.com/_ipx/f_png&s_2560x1440/images/hero/Microsoft-.NET-1366x768.png)

The Task Parallel Library in conjunction with the `async` and `await` keywords are great but there are some subtleties which you should consider. One of these is the use of the ConfigureAwait method.

If I wanted to get a list of the titles of the new posts from my RSS feed I could write the following code:

```cs
private async Task<IEnumerable<string>> GetBlogTitles()
{
    // Current Thread = UI Thread
    HttpClient httpClient = new HttpClient();

    // GetStringAsync = ThreadPool Thread
    string rss = await httpClient.GetStringAsync("https://rehansaeed.com/feed/");

    // Current Thread = UI Thread
    List<string> blogTitles = XDocument.Parse(rss)
        .Descendants("item")
        .Elements("title")
        .Select(x => x.Value)
        .ToList();

    // Current Thread = UI Thread
    return blogTitles;
}

public async Task UpdateUserInterface()
{
    // Current Thread = UI Thread
    IEnumerable<string> blogTitles = await this.GetBlogTitles();

    // Current Thread = UI Thread
    this.ListBox.ItemsSource = blogTitles;
}
```

If I was to call this method, then the entire method would execute on the calling thread except the bit where we call `GetStringAsync` which would go off and do its work on the ThreadPool thread and then we come back onto the original thread and do all our XML manipulation.

Now if this was a client WPF or WinRT application which has a UI thread, all of the XML manipulation we are doing would be done on the UI thread. This is placing extra burden on the UI thread which could mean application freeze ups if the UI thread is being heavily taxed. The solution is simple, we add `ConfigureAwait(false)` to the end of the call we are making to get the RSS XML. So now our new code looks like this:

```cs
private async Task<IEnumerable<string>> GetBlogTitles()
{
    // Current Thread = UI Thread
    HttpClient httpClient = new HttpClient();

    // GetStringAsync = ThreadPool Thread
    string rss = await httpClient.GetStringAsync("https://rehansaeed.com/feed/").ConfigureAwait(false);

    // Current Thread = ThreadPool Thread
    List<string> blogTitles = XDocument.Parse(rss)
        .Descendants("item")
        .Elements("title")
        .Select(x => x.Value)
        .ToList();

    // Current Thread = ThreadPool Thread
    return blogTitles;
}

public async Task UpdateUserInterface()
{
    // Current Thread = UI Thread
    IEnumerable<string> blogTitles = await this.GetBlogTitles();

    // Current Thread = UI Thread
    this.ListBox.ItemsSource = blogTitles;
}
```

So now all our XML manipulation is done on the `ThreadPool` thread along with the HTTP GET we are doing using the `HttpClient`. Notice however, that when we return the blog titles to the calling method we are back on the UI thread. Each time you do an `await`, the default behaviour is to continue on the thread we started with. By adding `ConfigureAwait(false)`, we are overriding this behaviour to continue on whatever thread the Task was running on.

For more on the Task Parallel Library (TPL) I highly recommend reading [Stephen Toub's](http://blogs.msdn.com/b/toub/) blog.

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 1 Comment

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/66)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![tusosp](https://camo.githubusercontent.com/16db1a1f329a3429739dccd208cfee5e1f25da6e97d73c01f0349773096254bf/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f64313063613864313133303163326634393933616332323739636534623933303f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/16db1a1f329a3429739dccd208cfee5e1f25da6e97d73c01f0349773096254bf/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f64313063613864313133303163326634393933616332323739636534623933303f733d393626643d6d6d26723d67) **tusosp** commented on **2017-12-11 09:30:23**

great post!!!

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/configureawait-task-parallel-library/#webpage","@type":"WebPage","dateModified":"2014-02-07T00:00:00+00:00","datePublished":"2014-02-07T00:00:00+00:00","description":"The importance of using ConfigureAwait when using the Task Parallel Library (TPL) to improve performance and reduce context switching.","name":"ConfigureAwait in Task Parallel Library (TPL)","url":"https://rehansaeed.com/configureawait-task-parallel-library/","breadcrumb":{"@id":"https://rehansaeed.com/configureawait-task-parallel-library/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/configureawait-task-parallel-library/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/configureawait-task-parallel-library/#article","@type":"Article","dateModified":"2014-02-07T00:00:00+00:00","datePublished":"2014-02-07T00:00:00+00:00","description":"The importance of using ConfigureAwait when using the Task Parallel Library (TPL) to improve performance and reduce context switching.","headline":"ConfigureAwait in Task Parallel Library (TPL)","inLanguage":"en-GB","url":"https://rehansaeed.com/configureawait-task-parallel-library/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/Microsoft-.NET-1366x768.png","caption":"Microsoft .NET","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/configureawait-task-parallel-library/#webpage"},"keywords":[".NET","C#","ConfigureAwait","HttpClient","Task Parallel Library","ThreadPool","Threads","TPL"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/configureawait-task-parallel-library/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/configureawait-task-parallel-library/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"ConfigureAwait in Task Parallel Library (TPL)","item":"https://rehansaeed.com/configureawait-task-parallel-library/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
