From 407750137966e580813e6961d510316cb588f51e Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Fri, 1 Apr 2022 15:52:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E4=BB=BB=E5=8A=A1=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/BackTaskTest.cs | 13 +++++++------ Falcon.SugarApi/BackTask/BackgroundLongTask.cs | 7 ++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Falcon.SugarApi.Test/BackTaskTest.cs b/Falcon.SugarApi.Test/BackTaskTest.cs index 1523a9e..997b47b 100644 --- a/Falcon.SugarApi.Test/BackTaskTest.cs +++ b/Falcon.SugarApi.Test/BackTaskTest.cs @@ -28,23 +28,23 @@ namespace Falcon.SugarApi.Test var asyncTask = new BackTaskAsync(); Console.WriteLine("启动异步任务"); asyncTask.StartAsync(token.Token).Wait(); - Thread.Sleep(5 * 1000); + Thread.Sleep(10 * 1000); Console.WriteLine("停止异步任务"); asyncTask.StopAsync(token.Token).Wait(); } } - public class BackTaskAsync : BackgroundLongTask + public class BackTaskAsync : BackTaskObject { public BackTaskAsync() { this.Log = new TestLog(); } - public ILogger Log { get; set; } - public override float RunTimespan => 1; - protected async override void Run() { + 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; } } @@ -76,8 +76,9 @@ namespace Falcon.SugarApi.Test base.OnCompleted(t); } - protected override void Run() { + protected override Task RunAsync() { this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test Run!"); + return Task.CompletedTask; } } diff --git a/Falcon.SugarApi/BackTask/BackgroundLongTask.cs b/Falcon.SugarApi/BackTask/BackgroundLongTask.cs index c494f35..2a37089 100644 --- a/Falcon.SugarApi/BackTask/BackgroundLongTask.cs +++ b/Falcon.SugarApi/BackTask/BackgroundLongTask.cs @@ -13,9 +13,9 @@ namespace Falcon.SugarApi.BackTask /// /// 瑕佹墽琛岀殑浠诲姟 /// - protected abstract void Run(); + protected abstract Task RunAsync(); /// - /// 鎵ц鏃堕棿闂撮殧 + /// 鎵ц鏃堕棿闂撮殧,绉 /// public abstract float RunTimespan { get; } /// @@ -46,7 +46,8 @@ namespace Falcon.SugarApi.BackTask OnStart(this); while (!TokenSource.Token.IsCancellationRequested) { await Task.Delay(TimeSpan.FromSeconds(RunTimespan)) - .ContinueWith(wt => { Run(); }).ContinueWith(wt => OnCompleted(this)); + .ContinueWith(_ => { RunAsync().Wait(); }) + .ContinueWith(_ => OnCompleted(this)); } OnStop(this); };