There is a program that at a specified time, on a timer, copies files. The copy time value is specified in the DateTimePicker (displays only time).
The time value from the DateTimePicker is written to the ScheduledTime variable, then it is compared with DateTime.Now, and if the ScheduledTime value is less, then 1 day is added. GetSetScheduledTime () function:
if (DateTime.Now > ScheduledTime) { while (DateTime.Now >= ScheduledTime) { ScheduledTime = ScheduledTime.AddDays(1); DateTimePickerTimer.Value = DateTimePickerTimer.Value.AddDays(1); } } The while loop is used to read the date from the config file, where the date may be old.
After refining ScheduledTime, we get the TickTime interval for the timer:
TickTime = (int)(GetSetScheduledTime() - DateTime.Now).TotalMilliseconds; if (TickTime >= 0) { TimerPl.Interval = TickTime; //Start Main Timer TimerPl.Start(); } After the timer is triggered, the TimerTick function stops the timer, starts the copy function, sends reports and logs and starts the timer function again, checks the time, turns out a new interval, and waits for the next trigger again.
The problem is that time floats. On the first day, on 2 different computers, the timer worked exactly, on the second day, it worked earlier for 1 second and 2 seconds. Whether this time will increase is not yet clear.
I understand that the timer is not an exact tool and tics can be delayed or go faster. But if we have a value from the DateTimePicker, where the time does not change, it only increases by 1 day and then this value is assigned to ScheduledTime and if every time I get a new interval that is equal to the planning time, the current time, should the error accumulate or can? How to avoid it? I do not mind backlash up to 5 seconds, but if this value will constantly increase, how to adjust the time?
UPD: In short, it was not possible to achieve the exact time, but since the task never stood, I have a backlash per second. I take this into account when forming the next date, reset all values where necessary and everything works. Sharp timers are not the best solution where you need accurate time. If possible, try to avoid them.