i ve used some ex from stackoverflow but sometimes my timer fire twice: this clock should fire each one minute, but just assume that it fires 00:00:59.666 then step1 = 59; step2 = 59; step3 = 1; return (1000-666); so it should fire not next minute but next 333 mili second, what can i do?
public int SyncTime = 60;
Clock = new System.Timers.Timer {AutoReset = false};
Clock.Elapsed += DoJob;
Clock.Interval = GetSync(SyncTime);
Clock.Start()
private void DoJob(object sender, ElapsedEventArgs elapsedEventArgs)
{
Clock.Interval = GetSync(SyncTime);
Clock.Start();
}
private static double GetSync(int syncTime)
{
DateTime now = DateTime.Now;
int step1 = (now.Minute * 60 + now.Second); //amount of miliseconds in this hour
int step2 = step1 % syncTime; //amount of miliseconds since last update
int step3 = syncTime - step2; //amount of miliseconds to next upadte
return (step3 * 1000 - now.Millisecond);
}