When we write such a for loop;
for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(() =>
{
Console.WriteLine(i.ToString());
});
}
We expect to get an unordered numbers between 0-9. However this is not the case. When you run this code, you will get such a result; (or similar but not as expected.)
10
10
10
10
10
10
10
10
10
May be this is obvious for some of you guys but there are many people out there trying to figure out what's wrong. Lets take a look to the next example to understand the problem easier.
for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
Console.WriteLine(i.ToString());
});
}
What we do here is initializing 10 tasks that will wait for a second and then print the value of i. That means value of i will be already 10 at the time our tasks reaches to the code Console.write(). This is exactly the same reason why first loop doesn’t work as expected because for loop finishes loop iteration before any tasks even starts.
An easy solution to fix this problem would be as follows;
for (int i = 0; i < 10; i++)
{
int temp = i;
Task.Factory.StartNew(() =>
{
Console.WriteLine(temp.ToString());
});
}
Now, we have the expected values.
0
2
3
4
5
6
1
8
9
7