I assume that you have already tried something, and that it is drifting.
If you want something to fire, say, every 5 seconds from an hour (5,10,15,20 ..), you could set your timer to fire once, then in your callback reset the fire timer to DateTime.Now + number of seconds until the next 5 second interval.
This will prevent any drift while your watch is correct.
Something like that
System.Timers.Timer timer = new Timer(); void Init() { timer.Elapsed += timer_Elapsed; int wait = 5 - (DateTime.Now.Second % 5); timer.Interval = wait*1000; timer.Start(); } void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { timer.Stop(); int wait = DateTime.Now.Second % 5; timer.Interval = wait * 1000; timer.Start(); }
Mikael svenson
source share