Consider the following code. I am starting with a task that does nothing, and then using ContinueWith() to start 10 calls to a method that increments a counter.
When I run this program, it prints “0”, indicating that the increment() method hasn’t been called at all. I was expecting it to be called 10 times, since that’s how many times I called ContinueWith().
If I uncomment the “Thread.Sleep(20)” line, then it prints “10” as expected.
This happens in either release or debug mode. My system is a core 2 quad with hyperthreading (8 logical cores) running Windows 7 x64.
I assume I have some kind of fundamental misunderstanding about how Task.ContinueWith() works….
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main()
{
using (var task = Task.Factory.StartNew(()=>{}))
{
for (int i = 0; i < 10; ++i)
{
task.ContinueWith(_=> increment());
// Thread.Sleep(20); // Uncomment to print 10 instead of 0.
}
task.Wait();
}
// This prints 0 UNLESS you uncomment the sleep above.
Console.WriteLine(counter);
}
static void increment()
{
Interlocked.Increment(ref counter);
}
private static int counter;
}
}
Can anyone shed any light on what’s going on here?
The reason is simple: You wait on the task that is already finished. What you really want is to wait for the ten tasks you created in the loop: