I am waiting on multiples task using Task.WhenAll. When one of them generates an exception I would like Task.WhenAll (or any other way of awaiting multiples tasks) to immediately cancel the others tasks and raise an exception.
Is it possible?
Thanks in advance
Cancellation is coopertive the
WhenAll
can't cancel the threads but you can pass all of them aCancellationToken
and fire the token when you get a exception.from inside the methods you will need to put
token.ThrowIfCancellationRequested()
inside the functions to check the token and cancel the taskNOTE: You could clean up the code a bit by making a extension method
This would make the code look like