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); };