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

  • Home
  • SEARCH
  • 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 8502227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:25:33+00:00 2026-06-11T01:25:33+00:00

I need to run a method with a given parameter in a thread. I’ve

  • 0

I need to run a method with a given parameter in a thread. I’ve noticed that when I run it,
the parameter is wrong. For the example given, I have an array int[] output with the numbers 1-7. For each number, I create a thread with the method WriteInt(i). I expect the output to be 1-7 in any order, but I consistently see some numbers missed and others duplicated. What is going on and what would the correct way be to start these threads?

(The list is only there to join the threads afterwards)

class Program
{
    static void Main(string[] args)
    {
        int[] output = { 1, 2, 3, 4, 5, 6, 7 };

        List<Thread> runningThreads = new List<Thread>();

        foreach (int i in output)
        {
            Thread thread = new Thread(() => WriteInt(i));
            thread.Start();
            runningThreads.Add(thread);
        }
        foreach(Thread t in runningThreads)
        {
            t.Join();
        }
    }

    private static void WriteInt(int i)
    {
        Console.WriteLine(i);
    }
}

Example output:

3
3
4
5
6
7
  • 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-11T01:25:35+00:00Added an answer on June 11, 2026 at 1:25 am

    The closure created by the lambda (() => WriteInt(i)) is getting closing over the variable i, not the value set to i within each iteration. As the thread runs, it uses the value set within i at that point in time, which is likely already been changed due to the foreach loop processing.

    You need a temporary:

    foreach (int i in output)
    {
            int temp = i;
            Thread thread = new Thread(() => WriteInt(temp));
            thread.Start();
            runningThreads.Add(thread);
    }
    

    For details on what’s happening, see Eric Lippert’s post titled Closing over the loop variable considered harmful.

    Also, in C# 5 (VS2012), this is no longer an issue for foreach loops. It will still happen with a for loop, however.

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

Sidebar

Related Questions

Possible Duplicate: Run a code in given time interval I have a method that
I need to run some method in Swing application in separate thread. What is
Why do we need to run the thread through start method and not directly
I need to run the application that I have made in Xcode on my
I have some methods that need to run as a certain service account, so
I have a fairly simple, though expensive, task that I need to run in
i need run code that will create a database and populate tables. i am
I need to run a script that will generate the projects.list file when a
The dilemma: I have options that need to store pricing data. Each option can
Given an array of lambdas and an array of values (both created at run

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.