Ali Sharabiani Ali Sharabiani - 27 days ago 12
C# Question

Create new threads in a for loop and pass parameters

Consider this code:

for(int i = 0; i < 10; i ++)
{
new Thread(() => Test(i)).Start();
}


The
Test(int i)
function:

public void Test(int i)
{
Console.WriteLine("=== Test " + i);
}


The actual output:


=== Test 3

=== Test 4

=== Test 4

=== Test 5

=== Test 5

=== Test 5

=== Test 9

=== Test 9

=== Test 9

=== Test 10


as you can see some numbers are missing and some other been duplicated.

Expected output:

I expect to see all numbers in random order.

Question

Should I lock any variables/methods? How can I fix this?

Answer

Should I lock any variables/methods? How can I fix this?

Your problem is with Closure and Captured Variables

Change your code as

for(int i = 0; i < 10; i ++)
{
    int tmp = i;
    new Thread(() => Test(tmp)).Start();
}

For more info: http://csharpindepth.com/articles/chapter5/closures.aspx or http://geekswithblogs.net/rajeevr/archive/2012/02/26/closures-and-captured-variable.aspx