Wms自定义定时服务代码完善

This commit is contained in:
alex
2023-08-03 15:18:00 +08:00
parent a07b3c73fc
commit adda03e0fb
9 changed files with 289 additions and 146 deletions

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tnb.Common.Extension
{
public static class TaskEx
{
public static Task<T> Catch<T, TError>(this Task<T> task, Func<TError, T> onError) where TError : Exception
{
var tcs = new TaskCompletionSource<T>(); // #A
task.ContinueWith(innerTask =>
{
if (innerTask.IsFaulted && innerTask?.Exception?.InnerException is TError)
tcs.SetResult(onError((TError)innerTask.Exception.InnerException)); // #B
else if (innerTask.IsCanceled)
tcs.SetCanceled(); // #B
else if (innerTask.IsFaulted)
tcs.SetException(innerTask?.Exception?.InnerException ?? throw new InvalidOperationException()); // #B
else
tcs.SetResult(innerTask.Result); // #B
});
return tcs.Task;
}
public static async Task Catch(this Task task, Action<Exception> exceptionHandler)
{
try
{
await task;
}
catch (Exception ex)
{
exceptionHandler(ex);
}
}
}
}