增加后台长期任务OnException处理异常

This commit is contained in:
falcon 2022-04-08 12:21:43 +08:00
parent f576b9ff34
commit 0bcaef1383

View File

@ -30,6 +30,10 @@ namespace Falcon.SugarApi.BackTask
/// 完成一次执行
/// </summary>
protected virtual void OnCompleted(BackgroundLongTask t) { }
/// <summary>
/// 执行中发生未处理异常
/// </summary>
protected virtual void OnException(BackgroundLongTask t, Exception ex) { }
/// <summary>
/// 用于定期执行任务的委托
@ -46,8 +50,15 @@ namespace Falcon.SugarApi.BackTask
OnStart(this);
while (!TokenSource.Token.IsCancellationRequested) {
await Task.Delay(TimeSpan.FromSeconds(RunTimespan))
.ContinueWith(_ => { RunAsync().Wait(); })
.ContinueWith(_ => OnCompleted(this));
.ContinueWith(async _ => {
try {
await RunAsync();
}
catch (Exception ex) {
OnException(this, ex);
}
})
.ContinueWith(t => { OnCompleted(this); });
}
OnStop(this);
};