From 7dfdf2e5194c305b328c43a37567625af2497d22 Mon Sep 17 00:00:00 2001 From: LongX Date: Fri, 15 Feb 2019 15:59:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OSS.Common/Interfaces/Job/BaseTimerTrigger.cs | 183 +++++++++--------- 1 file changed, 92 insertions(+), 91 deletions(-) diff --git a/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs b/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs index 8579067..9dba0d8 100644 --- a/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs +++ b/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs @@ -8,110 +8,111 @@ namespace OSS.Common.Interfaces.Job /// /// 定时器基础类 /// - public class BaseTimerTrigger:IDisposable + public class BaseTimerTrigger : IDisposable { private Timer _timer; - private readonly TimeSpan _dueTime; - private readonly TimeSpan _periodTime; + private readonly TimeSpan _dueTime; + private readonly TimeSpan _periodTime; - private readonly IJobExecutor _jobExcutor; + private readonly IJobExecutor _jobExcutor; - /// - /// 构造函数 - /// - /// 到期开始执行时间 - /// 间隔时间 - /// 任务执行者 - protected BaseTimerTrigger(TimeSpan dueTime, TimeSpan periodTime, IJobExecutor jobExcutor) - { - _dueTime = dueTime; - _periodTime = periodTime; - _jobExcutor = jobExcutor; - } - - - #region 基础方法 - - /// - /// 配置并触发定时器 - /// - /// - private void StartTimerTrigger() - { - if (_timer == null) - _timer = new Timer(ExcuteJob, _jobExcutor, _dueTime, _periodTime); - else - _timer.Change(_dueTime, _periodTime); - } - - /// - /// 停止定时器 - /// - private void StopTimerTrigger() - { - _timer?.Change(Timeout.Infinite, Timeout.Infinite); - } - - - - /// - public void Dispose() - { - _timer?.Dispose(); - } - - #endregion - - /// - /// 系统级任务执行启动 - /// - /// - /// - public virtual Task StartAsync(CancellationToken cancellationToken) - { - try + /// + /// 构造函数 + /// + /// 到期开始执行时间 + /// 间隔时间 + /// 任务执行者 + protected BaseTimerTrigger(TimeSpan dueTime, TimeSpan periodTime, IJobExecutor jobExcutor) { - StartTimerTrigger(); - } - catch (Exception e) - { - LogUtil.Error($"启动定时任务({nameof(GetType)})时出错,信息:{e}", string.Empty, "System_TimerJob"); + _dueTime = dueTime; + _periodTime = periodTime; + _jobExcutor = jobExcutor; } - return Task.CompletedTask; - } - /// - /// 系统级任务执行关闭 - /// - /// - /// - public virtual Task StopAsync(CancellationToken cancellationToken) - { - try - { - _jobExcutor.StopJob(); - StopTimerTrigger(); - } - catch (Exception e) - { - LogUtil.Error($"停止定时任务({nameof(GetType)})时出错,信息:{e}", String.Empty, "System_TimerJob"); - } - return Task.CompletedTask; - } + #region 基础方法 - private void ExcuteJob(object obj) - { - try + /// + /// 配置并触发定时器 + /// + /// + private void StartTimerTrigger() { - var excutor = obj as IJobExecutor; - excutor?.StartJob(); + if (_timer == null) + _timer = new Timer(ExcuteJob, _jobExcutor, _dueTime, _periodTime); + else + _timer.Change(_dueTime, _periodTime); } - catch (Exception e) + + /// + /// 停止定时器 + /// + private void StopTimerTrigger() { - LogUtil.Error($"执行任务({nameof(GetType)})时出错,信息:{e}", String.Empty, "System_TimerJob"); + _timer?.Change(Timeout.Infinite, Timeout.Infinite); + } + + + + /// + public void Dispose() + { + _timer?.Dispose(); + } + + #endregion + + /// + /// 系统级任务执行启动 + /// + /// + /// + public virtual Task StartAsync(CancellationToken cancellationToken) + { + try + { + StartTimerTrigger(); + } + catch (Exception e) + { + LogUtil.Error($"启动定时任务({nameof(GetType)})时出错,信息:{e}", string.Empty, "System_TimerJob"); + } + + return Task.CompletedTask; + } + + /// + /// 系统级任务执行关闭 + /// + /// + /// + public virtual Task StopAsync(CancellationToken cancellationToken) + { + try + { + _jobExcutor.StopJob(); + StopTimerTrigger(); + } + catch (Exception e) + { + LogUtil.Error($"停止定时任务({nameof(GetType)})时出错,信息:{e}", String.Empty, "System_TimerJob"); + } + + return Task.CompletedTask; + } + + private void ExcuteJob(object obj) + { + try + { + var excutor = obj as IJobExecutor; + excutor?.StartJob(); + } + catch (Exception e) + { + LogUtil.Error($"执行任务({nameof(GetType)})时出错,信息:{e}", String.Empty, "System_TimerJob"); + } } } } -}