From 1983778d2216f64742a506cdd70e652ac3d9973d Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Thu, 14 Jul 2022 10:14:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8E=E5=8F=B0=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=90=8C=E6=AD=A5=E6=89=A7=E8=A1=8C=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/BackTaskTest.cs | 13 +++++-- .../BackTask/BackgroundLongTask.cs | 37 ++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) 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; }