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