我有一个例程,假设每天早上 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/