Task.ContinueWith不工作我的预期

Mat*_*son 5 c# task

请考虑以下代码.我开始执行一个什么都不做的任务,然后使用ContinueWith()开始对一个递增计数器的方法进行10次调用.

当我运行该程序时,它打印"0",表示还没有调用increment()方法.我期待它被调用10次,因为那是我调用ContinueWith()的次数.

如果我取消注释"Thread.Sleep(20)"行,则按预期打印"10".

这在发布或调试模式下发生.我的系统是运行Windows 7 x64的超级线程(8个逻辑核心)的核心2 quad.

我假设我对Task.ContinueWith()如何工作有一些基本的误解....

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以了解这里发生的事情吗?

Dan*_*rth 9

原因很简单:您等待已经完成的任务.你真正想要的是等待你在循环中创建的十个任务:

var tasks = new List<Task>();
for (int i = 0; i < 10; ++i)
{
    tasks.Add(task.ContinueWith(_=> increment()));
}

Task.WaitAll(tasks.ToArray());
Run Code Online (Sandbox Code Playgroud)