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