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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:30:11+00:00 2026-05-31T01:30:11+00:00

Possible Duplicate: C# Captured Variable In Loop I am pretty new to multi-threading programming.

  • 0

Possible Duplicate:
C# Captured Variable In Loop

I am pretty new to multi-threading programming.
When I ran the code below and only the last child got executed.
Can some one tell me what happened?
Thank you very much.

private void Process()
{
    Dictionary<int, int> dataDict = new Dictionary<int, int>();
    dataDict.Add(1, 2000);
    dataDict.Add(2, 1000);
    dataDict.Add(3, 4000);
    dataDict.Add(4, 3000);

    foreach (KeyValuePair<int, int> kvp in dataDict)
    {
        Console.WriteLine("Ready for [" + kvp.Key.ToString() + "]");
        Task.Factory.StartNew(() => DoSomething(kvp.Value, kvp.Key));
    }

private static void DoSomething(int waitTime, int childID)
{
    {               
        Console.WriteLine("Start task [" + childID.ToString() + "]");
        Thread.Sleep(waitTime);
        Console.WriteLine("End task [" + childID.ToString() + "]");
    }
}

Output


Ready for [1]
Ready for [2]
Ready for [3]
Ready for [4]
Start task [4]
Start task [4]
Start task [4]
Start task [4]
End task [4]
End task [4]
End task [4]
End task [4]
  • 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-05-31T01:30:12+00:00Added an answer on May 31, 2026 at 1:30 am

    By using the loop variable in your lambda, all of the effectively refer to the same variable, which is the last item of your dictionary at the time they run.

    You need to assign the loop variable to another variable local to the loop before passing it to a lambda. Do this:

    foreach (KeyValuePair<int, int> kvp in dataDict)
    {
        var pair = kvp;
        Console.WriteLine("Ready for [" + pair.Key.ToString() + "]");
        Task.Factory.StartNew(() => DoSomething(pair.Value, pair.Key));
    }
    

    EDIT: It seems this little pitfall is fixed in C#5. That’s why it might work for others 😉 See comment by labroo

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

Sidebar

Related Questions

Possible Duplicate: How to use iPhone Camera as Bar Code Scanner? hi all How
Possible Duplicate: Opening url in new tab while i am doing window.open(), my page
Possible Duplicate: Storing echoed strings in a variable in PHP Suppose I have <?php
Possible Duplicate: Returning a value from thread? I have this code: //Asynchronously start the
Possible Duplicate: How an uninitialised variable gets a garbage value? So when an undefined
Possible Duplicate: OutOfMemoryError: bitmap size exceeds VM budget :- Android I have code like
Possible Duplicate: Calling virtual functions inside constructors Look at this code. In the constructor
Possible Duplicate: Can colorized output be captured via shell redirect? setup In this case
Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: How to handle diacritics (accents) when rewriting 'pretty URLs' I want to

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.