From fb3544ec89cce1f735b02250ab7c5572283642d5 Mon Sep 17 00:00:00 2001
From: Falcon <12919280+falconfly@user.noreply.gitee.com>
Date: Mon, 24 Feb 2025 11:08:58 +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=BC=80=E5=A7=8B=E5=90=8E=E5=92=8C=E6=AF=8F=E6=AC=A1?=
=?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=AE=8C=E6=88=90=E5=90=8E=E4=BB=BB=E5=8A=A1?=
=?UTF-8?q?=E9=80=80=E5=87=BA=E6=9C=BA=E5=88=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../TimedBackgroundTask/TimedTask.cs | 20 ++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs b/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs
index 8000939..285aaaf 100644
--- a/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs
+++ b/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs
@@ -54,17 +54,21 @@ namespace Falcon.SugarApi.TimedBackgroundTask
public DateTime NextTickTime { get; set; } = DateTime.MinValue;
///
- /// 后台任务开始
+ /// 后台任务开始。返回True任务会继续执行,False则任务退出。
///
- protected virtual void OnStart(TimedTask t,CancellationToken stoppingToken) { }
+ protected virtual bool OnStart(TimedTask t,CancellationToken stoppingToken) {
+ return true;
+ }
///
/// 后台任务停止
///
protected virtual void OnStop(TimedTask t,CancellationToken stoppingToken) { }
///
- /// 完成一次执行
+ /// 完成一次执行。返回True任务会继续执行,False则任务退出。
///
- protected virtual void OnCompleted(TimedTask t,CancellationToken stoppingToken) { }
+ protected virtual bool OnCompleted(TimedTask t,CancellationToken stoppingToken) {
+ return true;
+ }
///
/// 执行中发生未处理异常。返回True任务会继续执行,False则任务退出。
///
@@ -101,7 +105,9 @@ namespace Falcon.SugarApi.TimedBackgroundTask
/// 退出信号
/// 无返回
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
- this.OnStart(this,stoppingToken);
+ if(!this.OnStart(this,stoppingToken)) {
+ return;
+ }
while(await _timer.WaitForNextTickAsync(stoppingToken)) {
lock(_lock) {
if(stoppingToken.IsCancellationRequested) {
@@ -114,8 +120,8 @@ namespace Falcon.SugarApi.TimedBackgroundTask
}
try {
var goOn = await this.Run(stoppingToken);
- this.OnCompleted(this,stoppingToken);
- if(!goOn) {
+ var comResult = this.OnCompleted(this,stoppingToken);
+ if(!goOn || !comResult) {
break;
}
}