I'm tryng to execute some actions in parallel:
public static void Main()
{
var actions = new List<Action>();
for (int i = 0; i < 10; i++)
{
actions.Add(() => DoSomething(i));
}
Parallel.Invoke(actions.ToArray());
}
private static void DoSomething(int number)
{
Console.WriteLine(string.Format("DoSomething number {0}", number));
Thread.Sleep(5000);
}
But only the last action, with number parameter equals to 10 executed. In the output I got 10 times the same value:
DoSomething number 10