Reputation: 1696
I know that my goal can be achieved by using Task.WhenAny() but I will not deal with async await if I can avoid it, in order to prevent deadlocks. I have following code:
try
{
Task.WaitAny(this.Tasks.ToArray());
foreach (var task in this.Tasks)
{
if (task.IsFaulted || task.IsCanceled)
{
if (task.Exception.InnerException is OperationCanceledException)
{
}
}
}
}
catch (OperationCanceledException o)
{
// Handling cancelled tasks
}
catch (Exception e)
{
// Handling faulted tasks
}
And I for instance want to know exactly the id of my task, which has faulted or the Id of my task which has been cancelled. I have tried to do so as shown in the try block above, but this is not a solution since it also will throw an exception for tasks that has been cancelled before. Can I obtain a solution for this problem using Task.WaitAny() ?.
Upvotes: 1
Views: 1096
Reputation: 118987
From the documentation of Task.WaitAny
:
Return Value
Type: System.Int32
The index of the completed Task object in the tasks array.
So you can do this:
var taskIndex = Task.WaitAny(this.Tasks.ToArray());
var task = this.Tasks[taskIndex];
Upvotes: 4