private static void TaskException_AggregateExceptionCatched(object sender, AggregateExceptionEventArgs e) { foreach (var item in e.AggregateException.InnerExceptions) { Console.WriteLine("异常类型:{0}{1}来自:{2}{3}异常内容:{4}", item.GetType(), Environment.NewLine, item.Source, Environment.NewLine, item.Message); } }
public void EventHandlerCatchException() { Task t = new Task(() => { try { throw new InvalidOperationException("任务并行编码 中产生未知错误"); } catch (System.Exception ex) { AggregateExceptionEventArgs args = new AggregateExceptionEventArgs() { AggregateException = new AggregateException(ex) }; //使用主线程委托代理,处理子线程 异常 //这种方式没有阻塞 主线程或其他线程 AggregateExceptionCatched?.Invoke(null, args); } }); t.Start(); }