public static void Run(Action doWork, Action <Exception> errorAction = null) { try { Task task = new Task(() => { try { doWork?.Invoke(); } catch (Exception ex) { errorAction?.Invoke(ex); } }); task.Start(); } catch (Exception ex) { try { AggregateExceptionArgs args = new AggregateExceptionArgs() { AggregateException = new AggregateException(ex) }; AggregateExceptionCatched?.Invoke(null, args); } catch (Exception) { } } }
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(); }
public static void CatchAggregateException(object sender, AggregateExceptionArgs arg) { AggregateExceptionCatched?.Invoke(sender, arg); }