I have the following lines in my code:
var taskA = Task.Factory.StartNew(WorkA);
var taskB = Task.Factory.StartNew(WorkB);
var allTasks = new[] { taskA, taskB };
Task.Factory.ContinueWhenAll(allTasks, tasks => FinalWork(), TaskContinuationOptions.OnlyOnRanToCompletion);
But when I run this, I get the following error:
It is invalid to exclude specific continuation kinds for continuations off of multiple tasks.
Which is caused by the option TaskContinuationOptions.OnlyOnRanToCompletion.
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.
Based on @Peter Ritchie and @Ben McDougall answers I found a solution. I modified my code by removing redundant variable
tasksandTaskContinuationOptions.OnlyOnRanToCompletionWhere
FinalWorkis:If all
taskshave statusRanToCompletion, “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,