在TPL中排序任务

时间:2013-01-17 08:18:00

标签: c# asynchronous concurrency task-parallel-library concurrent-programming

如果我有以下代码

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static Task<int> GetSuperLargeNumber()
        {
            var main = Task.Factory.StartNew(() =>
                                                 {
                                                     Thread.Sleep(1000);
                                                     return 100;
                                                 });
            var second = main.ContinueWith(x => Console.WriteLine("Second: " + x.Result), TaskContinuationOptions.AttachedToParent);
            var third = main.ContinueWith(x => Console.WriteLine("Third: " + x.Result), TaskContinuationOptions.AttachedToParent);

            return main.ContinueWith(x  =>
                                             {
                                                 Task.WaitAll(second, third);
                                                 return x.Result;
                                             });
        }

        static void Main(string[] args)
        {
            GetSuperLargeNumber().ContinueWith(x => Console.WriteLine("Complete"));

            Console.ReadKey();
        }
    }
}

我希望main首先启动,然后2个依赖项可以在并行之后开始,它们是第一个和第二个。然后我想返回一个带有调用者值的未来来添加延续。但是我想确保第二个和第三个先跑。下面的代码是实现这一目标的最佳方法吗?看起来有点笨重

2 个答案:

答案 0 :(得分:3)

我对TPL不太熟悉,但这不是ContinueWhenAll的用途吗?

static Task<int> GetSuperLargeNumber()
{
    var main = Task.Factory.StartNew(() =>
                                         {
                                             Thread.Sleep(1000);
                                             return 100;
                                         });
    var second = main.ContinueWith(
        x => Console.WriteLine("Second: " + x.Result),
        TaskContinuationOptions.AttachedToParent);
    var third = main.ContinueWith(
        x => Console.WriteLine("Third: " + x.Result),
        TaskContinuationOptions.AttachedToParent);

    return Task.Factory.ContinueWhenAll(
        new[] { second, third },
        (twotasks) => /* not sure how to get the original result here */);
    }

我不知道如何从已完成的mainsecond(包含在third中)获得twotasks的结果,但也许您可以将其修改为通过结果。

编辑:或者,正如Alex指出的那样,使用

Task.Factory.ContinueWhenAll(new[] { main, second, third }, (threetasks) => ...

并阅读threetasks[0]的结果。

答案 1 :(得分:0)

这就足够了:

static Task<int> GetSuperLargeNumber()
{
    var main = Task.Factory.StartNew<int>(() =>
    {
        Thread.Sleep(1000);
        return 100;
    });
    var second = main.ContinueWith(x => Console.WriteLine("Second: " + x.Result), TaskContinuationOptions.AttachedToParent);
    var third = main.ContinueWith(x => Console.WriteLine("Third: " + x.Result), TaskContinuationOptions.AttachedToParent);

    Task.WaitAll(second, third);

    return main;
}