I have a routine which is suppose to run at 9am everyday....
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);
}
}
it runs at 9am then next run time it says 1.09:00:00 does this mean 9am tomorrow?