diff --git a/Falcon.SugarApi.Test/BackTaskTest.cs b/Falcon.SugarApi.Test/BackTaskTest.cs index 997b47b..b658250 100644 --- a/Falcon.SugarApi.Test/BackTaskTest.cs +++ b/Falcon.SugarApi.Test/BackTaskTest.cs @@ -41,11 +41,11 @@ namespace Falcon.SugarApi.Test } public override float RunTimespan => 1; - protected async override Task RunAsync() { + protected async override Task RunAsync() { this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:BackTaskAsync Run!"); //Thread.Sleep(3 * 1000); await Task.Delay(new TimeSpan(0,0,3)); - await Task.CompletedTask; + return true; } } @@ -76,9 +76,14 @@ namespace Falcon.SugarApi.Test base.OnCompleted(t); } - protected override Task RunAsync() { + protected override Task RunAsync() { + this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test RunAsync!"); + return Task.FromResult(true); + } + + protected override bool Run() { this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test Run!"); - return Task.CompletedTask; + return true; } } diff --git a/Falcon.SugarApi/BackTask/BackgroundLongTask.cs b/Falcon.SugarApi/BackTask/BackgroundLongTask.cs index 83af172..954be07 100644 --- a/Falcon.SugarApi/BackTask/BackgroundLongTask.cs +++ b/Falcon.SugarApi/BackTask/BackgroundLongTask.cs @@ -11,9 +11,15 @@ namespace Falcon.SugarApi.BackTask public abstract class BackgroundLongTask : BackgroundService, IDisposable { /// - /// 要执行的任务 + /// 要执行的任务,异步。 /// - protected abstract Task RunAsync(); + /// 继续执行True,退出False + protected virtual Task RunAsync() { return Task.FromResult(false); } + /// + /// 要执行的任务,同步。 + /// + /// 继续执行True,退出False + protected virtual bool Run() { return false; } /// /// 执行时间间隔,秒 /// @@ -36,7 +42,11 @@ namespace Falcon.SugarApi.BackTask protected virtual void OnException(BackgroundLongTask t, Exception ex) { } /// - /// 用于定期执行任务的委托 + /// 用于定期执行任务的委托,异步 + /// + private Action actionAsync; + /// + /// 用于定期执行任务的委托,同步 /// private Action action; @@ -46,13 +56,15 @@ namespace Falcon.SugarApi.BackTask /// 构造一个后台长期任务 /// public BackgroundLongTask() { - action = async () => { + actionAsync = async () => { OnStart(this); while (!TokenSource.Token.IsCancellationRequested) { await Task.Delay(TimeSpan.FromSeconds(RunTimespan)) .ContinueWith(async _ => { try { - await RunAsync(); + if (!await RunAsync()) { + TokenSource.Cancel(); + } } catch (Exception ex) { OnException(this, ex); @@ -62,6 +74,20 @@ namespace Falcon.SugarApi.BackTask } OnStop(this); }; + action = () => { + OnStart(this); + while (!TokenSource.Token.IsCancellationRequested) { + Thread.Sleep(TimeSpan.FromSeconds(RunTimespan)); + try { + if (!Run()) break; + OnCompleted(this); + } + catch (Exception ex) { + OnException(this, ex); + } + } + OnStop(this); + }; } /// /// 服务器启动时执行 @@ -69,6 +95,7 @@ namespace Falcon.SugarApi.BackTask /// 退出信号 /// protected override Task ExecuteAsync(CancellationToken stoppingToken) { + _ = Task.Factory.StartNew(actionAsync, stoppingToken); _ = Task.Factory.StartNew(action, stoppingToken); return Task.CompletedTask; }