I would like to understand how what I should be using inside of my "try" block when trying to await an Array of Tasks.
I want all tasks to be awaited, regardless if one of them threw an Exception, so that they can all complete.
Should I use:
var tasks = new Task<CasApiRouterModelExtendedInfo>[mbis.Length];
for (int i = 0; i < mbis.Length; i++)
{
tasks[i] = CAS.Service.GetAllRouterInterfacesAsync(mbis[i], false, 2);
}
try
{
Task.WaitAll(tasks);
}
catch (AggregateException ex)
{
Trace.TraceError("Some interface discoveries failed: ");
foreach (var innerEx in ex.InnerExceptions)
{
Trace.TraceError(innerEx.Message);
}
}
foreach (var task in tasks)
{
if (task.Status == TaskStatus.RanToCompletion && task.Result != null)
{
returnResults.Add(task.Result);
}
}
OR
var tasks = new Task<CasApiRouterModelExtendedInfo>[mbis.Length];
for (int i = 0; i < mbis.Length; i++)
{
tasks[i] = Task.Run(() => CAS.Service.GetAllRouterInterfacesAsync(mbis[i], true, 2));
}
try
{
for (int i = 0; i < tasks.Length; i++)
{
tasks[i].Wait();
}
}
catch (AggregateException ex)
{
Trace.TraceError("Some interface discoveries failed: ");
foreach (var innerEx in ex.InnerExceptions)
{
Trace.TraceError(innerEx.Message);
}
}
foreach (var task in tasks)
{
if (task.Status == TaskStatus.RanToCompletion && task.Result != null)
{
returnResults.Add(task.Result);
}
}
Also, does that "task.Status == TaskStatus.RanToCompletion" return true as long as the task didn't throw an Exception (is this a good way to do this check)?