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 8725931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:09:09+00:00 2026-06-13T08:09:09+00:00

If I fire off multiple async web requests, is it safe to append the

  • 0

If I fire off multiple async web requests, is it safe to append the result to a global variable, such as a StringBuilder? I know the order is not guaranteed.

  1. Does this cause a lot of Task blocking?

  2. Is this safe?

private static StringBuilder sb = new StringBuilder();

private static async Task AccessTheWebAsync()
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync(@"http://www.google.com/");
    sb.Append(response.StatusCode).Append(Environment.NewLine);        
}

static void Main(string[] args)
{
    List<Task> tasks = new List<Task>();
    for (int i = 0; i < 10; i++)
        tasks.Add(AccessTheWebAsync());
    Task.WaitAll(tasks.ToArray());
    Console.Write(sb.ToString());
    Console.ReadLine();
}
  • 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-13T08:09:10+00:00Added an answer on June 13, 2026 at 8:09 am

    Does this cause a lot of Task blocking?

    No. You will wait on all of the tasks to complete, but this is by design. Otherwise, this is a fairly efficient way to write this.

    Is this safe?

    This is not safe. As documented in StringBuilder: “Any instance members are not guaranteed to be thread safe.” (In this case, Append is an instance method.)

    Since you’re running this in a console application, the Append calls will happen on separate threads. You need some form of synchronization, such as using a lock statement around the Append call.

    Note that, if you were to use this same code in a Windows Forms or WPF application, however, the code which runs after await would be scheduled using the initial SynchronizationContext, which would cause it to always run on the UI thread, so you wouldn’t have thread synchronization issues. Since it’s in a console application, however, the continuations will run on separate threads.

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

Sidebar

Related Questions

I fire off about 2k async we service calls that usually take about a
I'm trying to fire off several HTTP requests from an ASP.NET page. The page
I'm attempting to fire off a JQuery .get() to a MVC (2 rc 2)
Does a PageAsyncTask fire off its own thread when used in a .aspx page?
I cannot get this function to fire off... I'm thinking I need a fresh
I am currently using an event to fire off a set of delegates, and
I have a very simply subclass of UIButton that will fire off a custom
I would like to fire off the server side selectedindexchanged method of a radgrid
I'm trying to fire off a WCF service call in a 100% asynchronous manner
I am making use of JQuery to fire off an AJAX request. As soon

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.