0
我有一個Windows服務設置爲在特定時間運行,它運行第一次,但我似乎無法解決如何告訴它運行第二天,當它結束次..在特定時間運行的Windows服務在第二天運行
這是我努力...
protected override void OnStart(string[] args)
{
log.Info("Info - Service Start");
string timeToRunStr = "17:11";
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");
break;
}
else {
TimeSpan test = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 16, 51, 0).Subtract(DateTime.Now);
nextRunTime = test;
}
}
_timer = new Timer((nextRunTime - currentTime).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);
}
}
爲什麼當一個簡單的控制檯應用程序+任務計劃程序可以開箱即用時,將所有邏輯融入到Windows服務中? – bluevector
theres發生的其他事情,我只需要鍛鍊這個失敗 – Beginner
一些如何添加24小時的測試時間跨度即時猜測 – Beginner