简体   繁体   English

如何使用任务并行库执行多个任务并在第一个任务之后返回以实际返回数据

[英]How do I execute multiple Tasks using Task Parallel Library and return after the first one to actually return data

I am working on an application in which getting data back to a serial process as fast as possible is important, but there can be multiple sources to get that data from. 我正在开发一个应用程序,在该应用程序中,尽快将数据返回串行过程很重要,但是可以有多个来源来获取数据。 As well, sometimes one source is faster than the other, but you don't know which source that will be. 同样,有时一个来源要比另一个来源快,但您不知道将是哪个来源。 I am using ContinueWhenAny(...).Wait() to await the first Task to end in order to continue and return out of the calling method. 我正在使用ContinueWhenAny(...)。Wait()等待第一个Task结束,以便继续并退出调用方法。 However, I need to check the validity of the data first, and only then return (or if all Tasks have finished and none of them have valid data). 但是,我需要先检查数据的有效性,然后才返回(或者如果所有任务都已完成并且它们都没有有效数据)。 Right now my code will return even invalid data if that's the Task that finishes first. 现在,如果那是最先完成的任务,我的代码甚至会返回无效数据。

Is there a way to do something like "ContinueWhenAny" but only when the Task.Result meets a certain condition, otherwise wait for the next task/etc.. until the last task finishes? 是否有一种方法可以执行类似“ ContinueWhenAny”的操作,但是只能在Task.Result满足特定条件时执行,否则请等待下一个任务等,直到最后一个任务完成为止。

As well, I need to make sure after one result is valid, that the other threads Cancel out. 同样,我需要确保一个结果有效后,其他线程才能取消。 This part is already working fine. 这部分已经可以正常工作了。

Currently, my code looks like this (stripped of exception handling, just the nuts and bolts): 当前,我的代码看起来像这样(剥离了异常处理,只是细节):

        ResultObject result = null;
        var tokenSource = new CancellationTokenSource();
        var tasks = listOfSources
                .Select(i => Task.Factory.StartNew(
                    () =>
                        {
                            i.CancellationToken = tokenSource.Token;
                            //Database Call
                            return i.getData(inputparameters);
                        }, tokenSource.Token));

        Task.Factory.ContinueWhenAny(
                tasks.ToArray(),
                firstCompleted =>
                    {
                        //This is the "result" I need to validate before setting and canceling the other threads
                        result = firstCompleted.Result;
                        tokenSource.Cancel();
                    }).Wait();
        return result;

Any ideas? 有任何想法吗? I don't want to use ContinueWhenAll since if the first call takes 2 seconds and the second takes 10 seconds, I want to get back to the serial process in 2 seconds if the first call returns valid data, otherwise wait 10 seconds, hope that result has valid data, and only return invalid data if all Tasks have completed and return an invalid result. 我不想使用ContinueWhenAll,因为如果第一次调用花费2秒,第二次调用花费10秒,那么如果第一次调用返回有效数据,我想在2秒内返回串行过程,否则请等待10秒,希望结果包含有效数据,并且仅在所有任务均已完成并且返回无效结果时才返回无效数据。

--------- UPDATE ---- Thanks zmbq for the great idea. ---------更新----感谢zmbq的好主意。 The updated (working) code is below and fulfills all my requirements. 更新后的(工作)代码在下面,并且满足我的所有要求。 One caveat however, the difference between this code and the previous code is that this code will return a null result if none of the Tasks produce a valid result, rather than the previous code which returns the invalid result itself. 请注意,此代码与先前的代码之间的区别在于,如果没有一个Task产生有效结果,则此代码将返回空结果,而不是先前的代码本身返回无效结果。 It wouldn't be difficult to change this version to do so too, but I'm perfectly content returning a null in that case for my purposes. 更改此版本也不难,但是出于我的目的,我很满意在这种情况下返回null。

        ResultObject result = null;
        var tokenSource = new CancellationTokenSource();
        var tasks = listOfSources
                .Select(i => Task.Factory.StartNew(
                    () =>
                        {
                            i.CancellationToken = tokenSource.Token;
                            //Database Call
                            return i.getData(inputparameters);
                        }, tokenSource.Token)).ToArray();

        result = GetFirstValidResult(tokenSource,tasks);

        return result;


   private ResultObject GetFirstValidResult(CancellationTokenSource tokenSource, Task<ResultObject>[] tasks)
    {
        ResultObject result = null;
        Task.Factory.ContinueWhenAny(
            tasks,
            firstCompleted =>
                {
                    var testResult = firstCompleted.Result;
                    if(testResult != null && testResult.IsValid())
                    {
                        result = testResult;
                        tokenSource.Cancel();
                    }
                    else
                    {
                        var remainingTasks = tasks.Except(new[]{firstCompleted}).ToArray();
                        if(remainingTasks.Any())
                        {
                            result = GetFirstValidResult(tokenSource, remainingTasks);
                        } 
                    }
                }).Wait();
        return result;
    }

Well, if your firstCompleted callback would check the result, and call ContinueWhenAny on the remaining tasks in case the result is illegal, you'd be done. 好吧,如果您的firstCompleted回调函数将检查结果,并在其余任务上调用ContinueWhenAny,以防结果不合法,那么您就可以完成。

As always, I recommend you take a look at ZeroMQ . 与往常一样,我建议您看一下ZeroMQ Fire the tasks and have each task write a message to an output queue if its result is legal. 触发任务,并让每个任务将消息写入输出队列(如果结果合法)。 The main thread will block on the queue and return when there's a valid message, or when all the tasks have finished already. 主线程将阻塞队列,并在收到有效消息或所有任务已经完成时返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM