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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:00:10+00:00 2026-05-23T17:00:10+00:00

doing a little experimenting to find out how things work. I have the following

  • 0

doing a little experimenting to find out how things work. I have the following code…

 for (int i = 0; i < 20; i++)
 {
      Task.Factory.StartNew(() => MethodTest(i));
 }

I’m wondering why MethodTest receives the int 20 almost always (unless I’m stepping through in debugger).

Obviously there’s something missing in my understanding as I assumed that when ‘i’ is passed it would be part of a managed thread’s local storage.

  • 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-23T17:00:11+00:00Added an answer on May 23, 2026 at 5:00 pm

    You are closing over the loop variable – try this:

     for (int i = 0; i < 20; i++)
     {
          int x = i;
          Task.Factory.StartNew(() => MethodTest(x));
     }
    

    The important thing to understand is that you are creating a closure over the variable i, and not its current value.

    By the time the thread pool is starting the first thread (they first land in the queue) the variable i will almost certainly be 20 as you have broken out of the loop. Now each thread that is started will look at the value of the variable i at that point in time.

    The fix as suggested is to create a new variable inside the scope of the loop and assign the current value of i to that variable. Since a new variable is used on every iteration of the loop each created thread is now closing over its “own” variable, which is isolated and will not change.

    The standard reference to explain what is going is “Closing over the loop variable considered harmful“.

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

Sidebar

Related Questions

I'm creating a game with points for doing little things, so I have a
I am working on a little hobby project and am experimenting with doing things
I have been doing a little work with regex over the past week and
I have the following query, which is doing very little and is an example
I've started doing a little freelance web work and I've got to build a
I am doing a little scripting and i find sometime more power would be
I am doing a little research into the feasibility of a project I have
Just doing a little touch up before finishing a conversion project and I have
I'm doing a little bit of work on a horrid piece of software built
I have been doing a little reading on Flow Based Programming over the last

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.