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