如何设置窗口服务的onStart()方法,以便安装后首先在上午12点执行,时间间隔工作正常,服务在上述时间间隔后执行,但不能在给定时间启动。
public static System.Timers.Timer Timer;
Double _timeinterval = 300 * 1000;// 6 mins
protected override void OnStart(string[] args)
{
Timer = new System.Timers.Timer();
Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
Timer.Interval = _timeinterval;
Timer.Enabled = true;
//method call to do operation
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
//method call to do operation
}
答案 0 :(得分:1)
protected override void OnStart(string[] args)
{
aTimer = new System.Timers.Timer();
string starttime = "01.25";
//start time is 01.25 means 01:15 AM
double mins = Convert.ToDouble(starttime);
DateTime t = DateTime.Now.Date.AddHours(mins);
TimeSpan ts = new TimeSpan();
// ts = t - System.DateTime.Now;
ts = t.AddDays(1) - System.DateTime.Now;
if (ts.TotalMilliseconds < 0)
{
ts = t.AddDays(1) - System.DateTime.Now;
// ts = t - System.DateTime.Now;
}
_timeinterval = ts.TotalMilliseconds;
// _timeinterval now set to 1:15 am (time from now to 1:15AM)
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = _timeinterval;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// operation to perform
aTimer.Interval = 86400000; // now interval sets to 24 hrs
}