From 8c13a3e225a463e562b252f858477611d16fa78c Mon Sep 17 00:00:00 2001 From: tq <1916474859@qq,com> Date: Mon, 24 Jun 2024 18:21:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/MyTask/CountDayOrderTask.cs | 40 +++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/Service/MyTask/CountDayOrderTask.cs b/Service/MyTask/CountDayOrderTask.cs index a3c7ff5..c29d736 100644 --- a/Service/MyTask/CountDayOrderTask.cs +++ b/Service/MyTask/CountDayOrderTask.cs @@ -16,6 +16,7 @@ public class CountDayOrderTask : ITask { private static readonly ILog Log = LogManager.GetLogger(typeof(CountDayOrderTask)); private volatile bool _stop; + private Timer _timer; public ChargeOrderService _ChargeOrderService { get; set; } @@ -29,13 +30,14 @@ public class CountDayOrderTask : ITask public int Interval() { - return 1000 * 86400; + return 1000 * 10; } public void Handle() { // 统计每日运行 - CountOrder(); + return; + // CountOrder(); } @@ -43,16 +45,50 @@ public class CountDayOrderTask : ITask public bool Stoped() { return _stop; + } public void Stop() { _stop = true; + _timer?.Change(Timeout.Infinite, Timeout.Infinite); + } public void ResetStop() { _stop = false; + ScheduleNextRun(); + + } + public CountDayOrderTask() + { + ScheduleNextRun(); + } + + private void ScheduleNextRun() + { + if (_stop) + { + return; + } + + DateTime now = DateTime.Now; + DateTime nextRun = new DateTime(now.Year, now.Month, now.Day, 19, 21, 0); + if (now > nextRun) + { + nextRun = nextRun.AddDays(1); + } + + TimeSpan timeToGo = nextRun - now; + _timer = new Timer(x => + { + if (!_stop) + { + CountOrder(); + ScheduleNextRun(); + } + }, null, timeToGo, Timeout.InfiniteTimeSpan); } public void CountOrder()