我有以下代码可以触发多个异步任务。
List<TimeoutException> TimeoutExceptions = new List<TimeoutException>();
List<TaskCanceledException> TaskCanceledExceptions = new List<TaskCanceledException>();
List<Exception> Exceptions = new List<Exception>();
List<AggregateException> AggregateExceptions = new List<AggregateException>();
List<Models.Channel.IChannel> channels = new List<Models.Channel.IChannel>();
channels.Add(new Models.Channel.DummyChannelName());
var tasks = new List<Task>();
foreach (Models.Channel.IChannel channel in channels)
{
try
{
var cts = new CancellationTokenSource();
cts.CancelAfter(channel.TimeOut);
tasks.Add(Task.Run(() =>
{
channel.Data = channel.RequestOffers(new Models.Request.AvailabilityRequest()).Result;
if (cts.Token.IsCancellationRequested)
cts.Token.ThrowIfCancellationRequested();
}, cts.Token));
}
catch (TimeoutException t)
{
TimeoutExceptions.Add(t);
}
catch (TaskCanceledException tc)
{
TaskCanceledExceptions.Add(tc);
}
catch (AggregateException ae)
{
AggregateExceptions.Add(ae);
}
catch(Exception ex)
{
Exceptions.Add(ex);
}
}
Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5));
我遇到的问题是,如果由于超时而取消任务,我将收到以下异常
<ExceptionMessage>One or more errors occurred.</ExceptionMessage>
<ExceptionType>System.AggregateException</ExceptionType>
是否只是一个简单的案例,我需要围绕Task.WaitAll进行Try Catch,还是应该以不同的方式构造我的代码?
答案 0 :(得分:1)
如果任务内发生异常,则Wait
会在任务上在调用线程上引发异常。如果成功创建了任务,则该任务内部发生的所有异常都将被封装在AggregateException
中并等待。
以您为例,这意味着您可以在循环中删除try / catch块,并在循环后使用它包装Task.WaitAll(...)
。
var tasks = new List<Task>();
foreach (Models.Channel.IChannel channel in channels)
{
Task myTask = Task.Run(...); // create your task here
tasks.Add(myTask);
}
try
{
Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5));
}
catch
{
// Insert Exception handling logic
}