Does System.Threading.Timer use the correct system time?

I have a service that runs a task every 30 minutes based on System.Threading.Timer. The timer is set at intervals, as usual, and disables operation asynchronously.

Last night, on the server on which he lives, the decision was made to reset himself something completely different from the actual time and date. Subsequently, my tasks did not start every 30 minutes - they completely stopped.

However, after resetting the time this morning, the next 30-minute task started (30 minutes later), and then ALL other tasks performed simultaneously, as if they had been in the queue all night.

Can anyone shed some light on this?

EDIT: as an update - using a different timer (System.Timers.Timer), and it happened the same way. Time changes to something completely different, the service then stops performing its tasks until the time is lower than reset in the morning, and then after 30 minutes ALL tasks will be performed that must be performed every 30 minutes from the moment of change!

+6
source share
4 answers

I can not reproduce the script that leads to this.

Using this code

class Program { private static Timer timer; private static readonly Stopwatch stopwatch = new Stopwatch(); static void Main(string[] args) { timer = new Timer(Tick); stopwatch.Start(); timer.Change(30000, Timeout.Infinite); Console.ReadLine(); } private static void Tick(object obj) { stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed.Seconds); } } 

I launched the application, changing the time of my local computer to 20 seconds in the future. The Tick method is still called 30 seconds after I started the application. The stopwatch says 30, and my mobile timer says 30.

+1
source

I do not know how this could happen, but I know that Microsoft says the following: "For server-side timer functions, you can use System.Timers.Timer . "

0
source

I think that since the timer invokes callbacks to be executed by threads in the thread pool, and perhaps it schedules a callback for the actual time in the future, not the interval.

@GertArnold's answer is correct, you should use System.Timers.Timer instead, because it raises the Elapsed event based on the value of the Interval property.

0
source

Since you have tried all the corresponding built-in timer parameters, only the following parameters remain:

  • use Sleep is what I see as bad practice, but in your specific case you need to solve the problem described

  • find some third-party timer library and test it with your special special conditions ...

0
source

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


All Articles