Task异常捕捉

public static Task Catch(this Task task,Action<Exception> callback)
{
return task.ContinueWith<Task>(delegate (Task t)
{
if (t != null && t.IsFaulted)
{
AggregateException exception = t.Exception;
Trace.TraceError("Catch exception thrown by Task: {0}", new object[]
{
exception
}); 
callback(exception.InnerException);
}
return t;
}).Unwrap();
}

 

posted @ 2020-09-22 13:20  Observer_Dep  阅读(62)  评论(0编辑  收藏  举报