c# 执行 1.09 :00:00 timespan mean 9am tomorrow?

标签 c# datetime timer timespan

我有一个例程,假设每天早上 9 点运行....

protected override void OnStart(string[] args)
{
   log.Info("Info - Service Start");
   string timeToRunStr = "09:00";
   var timeStrArray = timeToRunStr.Split(';');
   foreach (var strTime in timeStrArray)
   {
        timeToRun.Add(TimeSpan.Parse(strTime));
   }
    ResetTimer();
}

void ResetTimer()
{
   log.Info("Info - Reset Timer");
   try
   {
       TimeSpan currentTime = DateTime.Now.TimeOfDay;
       TimeSpan nextRunTime = timeToRun[0];
       foreach (TimeSpan runTime in timeToRun)
       {
           if (currentTime < runTime)
           {
               nextRunTime = runTime;
               // log.Info("Info - in loop");
               _timer = new Timer((nextRunTime - currentTime).TotalSeconds * 1000);
               break;
           }
           else {
             TimeSpan test = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 16, 51, 0).AddDays(1).Subtract(DateTime.Now);
             nextRunTime = test;
              _timer = new Timer((nextRunTime).TotalSeconds * 1000);
            }
        }               
        log.Info("Info - Timer : " + (nextRunTime - currentTime).TotalSeconds * 1000);
        log.Info("Info - nextRuntime : " + nextRunTime);
        log.Info("Info - currentTime : " + currentTime);
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        _timer.Start();
    }
    catch (Exception ex)
    {
        log.Error("This is my timer error - ", ex);
    }
}

它在上午 9 点运行,然后下一个运行时间显示 1.09:00:00 这是否意味着明天上午 9 点?

最佳答案

1.09:00:00表示1天9小时的时间跨度

关于c# 执行 1.09 :00:00 timespan mean 9am tomorrow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115195/

相关文章:

c# - 单击按钮时弹出

c# - 编写 .NET 对象脚本

c# - 自定义字符串的 TimeSpan 转换

java - DateTimeFormatter 创建模式

c - PIC16F883 定时器不工作

c# - WebAPI + AngularJS 中基于 DateTime 的动态 HTTP 请求

neovim/vim8 的 C# LSP 自动补全客户端

c# - 在 c#.net 中对绑定(bind)到数据表的 gridview 进行排序

javascript - 下一个营业季度的输出日期和计数

ios - 我怎样才能停止计时器?