How to check that all tasks have been properly completed?

别说谁变了你拦得住时间么 提交于 2019-12-05 05:18:13
Zen

Based on @Peter Ritchie and @Ben McDougall answers I found a solution. I modified my code by removing redundant variable tasks and TaskContinuationOptions.OnlyOnRanToCompletion

var taskA = Task.Factory.StartNew(WorkA);
var taskB = Task.Factory.StartNew(WorkB);
var allTasks = new[] { taskA, taskB };
Task.Factory.ContinueWhenAll(allTasks, FinalWork);

Where FinalWork is:

private static void FinalWork(Task[] tasks)
{
   if (tasks.All(t => t.Status == TaskStatus.RanToCompletion))
   {
        // do "some work"
   }
}

If all tasks have status RanToCompletion, "some work" will be done. It will be performed immediately after all tasks have completed and will not block the main task. If I cancel at least one of the tasks, nothing will be done.

Alternatively you can do this,

var taskA = Task.Factory.StartNew(WorkA);
var taskB = Task.Factory.StartNew(WorkB);
var allTasks = new[] { taskA, taskB };
var continuedTask = Task.WhenAll(allTasks).ContinueWith((antecedent) => { /*Do Work*/ }, TaskContinuationOptions.OnlyOnRanToCompletion));

You haven't provided any code that does anything with any of the tasks that ran to completion (your tasks variable is ignored). You'd get the same result if you simply removed TaskContinuationOptions.OnlyOnRanToCompletion. i.e. If you could use ContinueWhenAll with TaskContinuationOptions.OnlyOnRanToCompletion your continuation isn't going to be called until all tasks have either completed or failed. If you don't do anything with just the completed tasks, that's the same as Task.Factory.ContinueWhenAll(allTasks, tasks => FinalWork());

If there's something more specific that you want to do, please provide the details so that someone might be able to help you.

To answer the actual question you posed:

My question is how to check that all tasks have done their work properly (all tasks statuses are RanToCompletion) and then do FinalWork()? In the meantime, the application performs other tasks.

at least that is what I read as the question check the following code:

    var taskA = Task.Factory.StartNew(WorkA);
    var taskB = Task.Factory.StartNew(WorkB);
    var allTasks = new[] { taskA, taskB };

    taskA.Wait();
    taskB.Wait();

    if (taskA.Status == TaskStatus.RanToCompletion && taskB.Status == TaskStatus.RanToCompletion)
        Task.Factory.ContinueWhenAll(allTasks, tasks => FinalWork());
    else
        //do something

You actually answered the question yourself with your question if you did mean that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!