I would like to understand how I should use "try" inside my block when trying to find an array of tasks.
I want all the tasks to be expected, despite the fact that one of them threw an exception so that everyone could 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 this "task.Status == TaskStatus.RanToCompletion" return true if the task did not throw an exception (is this a good way to do this check)?
source
share