Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8684441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:18:26+00:00 2026-06-12T22:18:26+00:00

I would like put the code first and then explain the situation and ask

  • 0

I would like put the code first and then explain the situation and ask my question based on that:

public partial class MainWindow : Window {

    public MainWindow() {
        InitializeComponent();
    }

    private async void Button_Click_2(object sender, RoutedEventArgs e) {

        var result = await GetValuesAsync();
        Foo.Text += result;
    }

    public async Task<string> GetValuesAsync() {           

        using (var httpClient = new HttpClient()) {

            var response = await httpClient
                .GetAsync("http://www.google.com")
                .ConfigureAwait(continueOnCapturedContext: false);


            // This is the continuation for the httpClient.GetAsync method.
            // We shouldn't get back to sync context here
            // Cuz the continueOnCapturedContext is set to *false*
            // for the Task which is returned from httpClient.GetAsync method
            var html = await GetStringAsync();

            // This is the continuation for the GetStringAsync method.
            // Should I get back to sync context here?
            // Cuz the continueOnCapturedContext is set to *true*
            // for the Task which is returned from GetStringAsync 

            // However, GetStringAsync may be executed in another thread
            // which has no knowledge for the sync context 
            // because the continueOnCapturedContext is set to *false*
            // for the Task which is returned from httpClient.GetAsync method.

            // But, on the other hand, GetStringAsync method also has a 
            // chance to be executed in the UI thread but we shouldn't be
            // relying on that. 
            html += "Hey...";
            Foo.Text = html;

            return html;
        }
    }

    public async Task<string> GetStringAsync() {

        await Task.Delay(1000);
        return "Done...";
    }
}

This is a fairly simple WPF sample which runs on .NET 4.5 and probably doesn’t make a lot of sense but this should help me explain my situation.

I have a button on the screen which has an asynchronous click event. When you look at the GetValuesAsync code, you will see the usage of await keyword twice. With the first usage, I set continueOnCapturedContext parameter of the Task.ConfigureAwait method to false. So, this indicates that I don’t necessarily want my continuation to be executed inside the SynchronizationContext.Current. So far so good.

At the second await usage (with the GetStringAsync method), I didn’t call the ConfigureAwait method. So, I basically indicated that I want to get back to current synchronization context for the continuation of the GetStringAsync method. So, as you can see, I try to set the TextBlock.Text (which belongs to UI thread) property inside the continuation.

When I run the application and click the button, I get an exception giving me the following message:

The calling thread cannot access this object because a different
thread owns it.

At first, this made no sense to me and I thought that I discovered a bug but then, I realized that GetStringAsync may be executed in another thread (highly likely) which is different than the UI thread and has no knowledge for the sync context because the continueOnCapturedContext is set to false for the Task which is returned from httpClient.GetAsync method.

Is this the case here? Also, in this case, is there a chance for GetStringAsync method to be posted back to UI thread bacuse the httpClient.GetAsync method continuation may be executed inside the UI thread?

I have also a few comments inside the code. In view of my questions and the comments inside the code, am I missing anything here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:18:28+00:00Added an answer on June 12, 2026 at 10:18 pm

    When you call ConfigureAwait(false), the rest of the method will be executed on a thread pool thread unless the Task you’re awaiting is already complete.

    Since GetAsync will almost definitely run asynchronously, I would expect GetStringAsync to run on a thread pool thread.

    public async Task<string> GetValuesAsync() {           
    
        using (var httpClient = new HttpClient()) {
    
            var response = await httpClient
                .GetAsync("http://www.google.com")
                .ConfigureAwait(continueOnCapturedContext: false);
    
            // And now we're on the thread pool thread.
    
            // This "await" will capture the current SynchronizationContext...
            var html = await GetStringAsync();
            // ... and resume it here.
    
            // But it's not the UI SynchronizationContext.
            // It's the ThreadPool SynchronizationContext.
            // So we're back on a thread pool thread here.
    
            // So this will raise an exception.
            html += "Hey...";
            Foo.Text = html;
    
            return html;
        }
    }
    

    Also, in this case, is there a chance for GetStringAsync method to be posted back to UI thread bacuse the httpClient.GetAsync method continuation may be executed inside the UI thread?

    The only way GetStringAsync will run on the UI thread is if GetAsync completes before it’s actually awaited. Highly unlikely.

    For this reason, I prefer to use ConfigureAwait(false) for every await once the context is no longer needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I would like to have an easy code that creates a cookie called
I would like to put my local OS X wordpress site in my ~users/Sites/public
Imagine a user that would like to put a form on their website that
I have a watermark that I would like to put into my pdf. The
I have a class library written in .Net that I would like to make
First of all I would like to apologize for the question itself. I simply
I would like to put newlines in a property value of a manifest. Its
I would like to put a button on top of a Google Map next
I would like to put a placeholder into e.g. the footer.html and have it
I would like to put jQuery at the bottom of the body tag but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.