1

I'm starting with the new .net 4.5 async programming and I found a situation like the code below: I have a sync method but I would like to make several calls and make it run in parallel. However, in this code, all the calls to the sync method runs with the id = 10 and I'm not sure why (probably I misunderstand something with this new approach :).

class Program
{
    static void Main(string[] args)
    {
        var tasks = new List<Task>();

        for (int i = 0; i < 10; i++)
        {
            var foo = new Foo();
            var fooTask = Task.Run(() => foo.FooNonAsyncMethod(i));
            tasks.Add(fooTask);
        }
        tasks.ForEach(t => t.Wait());

        Console.WriteLine("All Done!");

        Console.ReadLine();
    }
}

public class Foo
{
    public void FooNonAsyncMethod(int id)
    {
        Console.WriteLine("Starting {0}", id);

        Thread.Sleep(4000);

        Console.WriteLine("Ending {0}", id);
    }
}

// Output:

// Starting 10
// Starting 10
// Starting 10
// Starting 10
// Ending 10
// Starting 10
// Starting 10
// Ending 10
// Ending 10
// ...
Max Bündchen
  • 1,283
  • 17
  • 38
  • 2
    possible duplicate of [Is there a reason for C#'s reuse of the variable in a foreach?](http://stackoverflow.com/questions/8898925/is-there-a-reason-for-cs-reuse-of-the-variable-in-a-foreach) – Yurii Aug 28 '14 at 12:00
  • 1
    @Yuriy That is not a duplicate because it is about `foreach` here it is `for`. But definitely that answers the question. – Sriram Sakthivel Aug 28 '14 at 12:01
  • 2
    Use `Task.WaitAll` instead of `tasks.ForEach(t => t.Wait());` – Sriram Sakthivel Aug 28 '14 at 12:02

1 Answers1

4

That's because there is only 1 variable i and the lambda expressions bind on a variable and not a value.

You can fix this by using:

for (int i = 0; i < 10; i++)
{
    int newI = i;
    var foo = new Foo();
    var fooTask = Task.Run(() => foo.FooNonAsyncMethod(newI));
    tasks.Add(fooTask);
}

As @Yuriy mentioned, this answer has a lot more info on this particularity : Is there a reason for C#'s reuse of the variable in a foreach?

Community
  • 1
  • 1
Bun
  • 1,465
  • 10
  • 23