Continuation when one or more tasks in an array is cancelled or fails?

241 views Asked by At

If I have an array of tasks, Task[]. How can I write a continuation that only runs when one or more tasks in the array fails(or is cancelled)?

1

There are 1 answers

0
Slugart On

I think you should look at the continuation options that you can specify when you set the continuation for a Task.

Task<int> [] tasks = new Task<int>[5];
// Add tasks...

foreach (var task in tasks)
{
    task.ContinueWith(a => a.Id, TaskContinuationOptions.OnlyOnCanceled);
}
Task.WaitAny(tasks, new CancellationToken());