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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:38:44+00:00 2026-06-17T10:38:44+00:00

I started to learn Asynchronous programming in C# 5 and .Net 4.5 and there

  • 0

I started to learn Asynchronous programming in C# 5 and .Net 4.5 and there is something I don’t understand.

private static int count;

public static void Main()
{
    LoopTest();

    Console.ReadKey(false);
}

static async void LoopTest()
{
    count = 0;

    for (var index = 0; index < 10; ++index)
    {
        Console.WriteLine( "({0}) In Loop before await, Index {1}, Thread: {2}", count++, index, Thread.CurrentThread.ManagedThreadId);
        await Task.Factory.StartNew(() => Thread.Sleep(10));
    }
}

The result is:

 (0) In Loop before await, Index 0, Thread: 9  
 (1) In Loop before await, Index 1, Thread: 10    
 (2) In Loop before await, Index 2, Thread: 11   
 (3) In Loop before await, Index 3, Thread: 10  
 (4) In Loop before await, Index 4, Thread: 11  
 (5) In Loop before await, Index 5, Thread: 10  
 (6) In Loop before await, Index 6, Thread: 12  
 (7) In Loop before await, Index 7, Thread: 11  
 (8) In Loop before await, Index 8, Thread: 10  
 (9) In Loop before await, Index 9, Thread: 12

So are there different Threads accessing the same loop? is there a Race-Condition on the index variable?

  • 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-17T10:38:46+00:00Added an answer on June 17, 2026 at 10:38 am

    No, there is no race condition. You start a new task, that will run in the background, but your code will not continue until that task finishes, so you can be sure that your code will only be executed in one thread at any one time.

    However, because this is a console application there is no current SyncronizationContext. (As opposed to a winform, WPF, ASP, Silverlight, etc. application in which there will be.) This means that the continuations from any await call will run in the thread pool, not in the calling thread. So yes, you’re running in one of the thread pool threads in each iteration of the loop, and it could be a different one each time, but you can be sure that, excepting whatever you put inside the new task, you will “finish” running on any given thread before you start working on the next, which ensures that there is only ever one thread running code at any given time (which means no race conditions).

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

Sidebar

Related Questions

I recently started to learn Java programming and we just reach the Java Threads
Started to learn programming 2 months ago, in one of my little projects i
I just started to learn SVN. Please give me your advice to understand the
Hi I recently started to learn asp.net webforms and I noticed that the code
I have just started to learn the very basics of Java programming. Using a
I just started to learn Android development. My previous experience is majorly .NET framework
I have started to learn ASP.NET MVC, and at this time of studying I
I started to learn objective-c and ios programming.I have two View Controller which are
I've just started to learn C. I'm using Dev C++ but I don't like
I have just started to learn asp.net MVC3 and i run into this problem:

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.