增加后台任务开始后和每次执行完成后任务退出机制
This commit is contained in:
parent
3560ab0e54
commit
fb3544ec89
|
@ -54,17 +54,21 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
|||
public DateTime NextTickTime { get; set; } = DateTime.MinValue;
|
||||
|
||||
/// <summary>
|
||||
/// 后台任务开始
|
||||
/// 后台任务开始。返回True任务会继续执行,False则任务退出。
|
||||
/// </summary>
|
||||
protected virtual void OnStart(TimedTask t,CancellationToken stoppingToken) { }
|
||||
protected virtual bool OnStart(TimedTask t,CancellationToken stoppingToken) {
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 后台任务停止
|
||||
/// </summary>
|
||||
protected virtual void OnStop(TimedTask t,CancellationToken stoppingToken) { }
|
||||
/// <summary>
|
||||
/// 完成一次执行
|
||||
/// 完成一次执行。返回True任务会继续执行,False则任务退出。
|
||||
/// </summary>
|
||||
protected virtual void OnCompleted(TimedTask t,CancellationToken stoppingToken) { }
|
||||
protected virtual bool OnCompleted(TimedTask t,CancellationToken stoppingToken) {
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行中发生未处理异常。返回True任务会继续执行,False则任务退出。
|
||||
/// </summary>
|
||||
|
@ -101,7 +105,9 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
|||
/// <param name="stoppingToken">退出信号</param>
|
||||
/// <returns>无返回</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user