Timers that synchronize events with the system clock

I would like to do some work every 5 seconds. When I speak every 5 seconds, I mean 00:00, 00:05, 00:10, 00:15, 00:20 according to the system clock. Not every 5 seconds from the start of the timer.

Is there a .net timer that can do this already - I can't find it?

I can think of two works around ...

  • Have a timer with a very high resolution and read the system clock of each timer event. Fire my work right after every whole 5 seconds.

  • Have a timer with a very high resolution and read the system clock of each timer event. Turn off the second timer, which has a resolution of 5 seconds immediately after each full 5 seconds.

I do not like any of the approaches - the first puts an unnecessary load on the processor (depending on how high the ratio is). The second method can drop in after a very long time. I don’t know if this is true or not.

+3
source share
2 answers

Not directly, but you can set one short timer to start in the next 5-fold, in which case the handler will adjust the schedule every 5 seconds.

+1
source

All ordinary timers (WinForms, Threading, Timer) have a resolution of ~ 20 ms.

You can use any of them in a single mode and count the time in ms until the next 5 seconds. Expect 0.100 ms error.

, , , .

, / N * 20ms .

+1

Source: https://habr.com/ru/post/1760502/


All Articles