我试图让计时器每分钟运行一次,与系统时钟(00:01:00、00:02:00、00:03:00 等)同步。下面的代码会产生一个稳定的计时器吗?我试过运行它。计时器确实每分钟精确运行一次。但问题是它会在几个月后开始徘徊吗?
private System.Timers.Timer timer;
public frmMain()
{
timer = new System.Timers.Timer();
timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = GetInterval();
timer.Start();
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//Do processing
timer.Interval = GetInterval();
timer.Start();
}
private double GetInterval()
{
DateTime now = DateTime.Now;
return ((60 - now.Second) * 1000 - now.Millisecond);
}