1

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

Click here for fiddle example

user3607621
  • 169
  • 2
  • 8

0 Answers0