System.Threading.Timer and 49.7 day timer

Is it safe to use System.Threading.Timer on a Windows service running for a long time?

In other words, is the 49.7 day timer loading issue fixed in Windows Server 2003 32bit + .Net Framework 2.0?

Is Thread.Sleep () the only alternative?

Thank.

+2


source to share


2 answers


The internal periods are advertised as UInt32

having MaxValue

out of 4,294,967,295, or in other words 49.7 days ... so I don't believe it will work correctly for such a long period. But, if I look at the reflected code, it should work if you destroy the timer every fe 5 days and create a new one.



Edit: As I think about it, timers and services don't mix well with each other, you can read this and this .

+1


source


The problem with polling the Windows system time does not affect System.Threading.Timer

or System.Timers.Timer

(this is just a shell around System.Threading.Timer

).

The maximum value you can use for the timer interval is int.MaxValue

, or 2147483648 milliseconds (24.86 days). But this is only the maximum one interval you can specify. This does not mean that your longest timer can run for 25 days. That is, if you have a timer that sets the tick once every 30 seconds, it will keep doing so for as long as your program is running. The timer will not pass out after 25 days and will miss the check mark. And he won't lose his mind after 49.7 days.

The reason is that these timers don't even look at the system time ( Environment.TickCount

). These are interval timers.



The idea that timers and services don't pair well with each other is laughable. You don't want to use it System.Windows.Forms.Timer

in a service because this timer depends on having a message loop. But System.Threading.Timer

they System.Timers.Timer

will also work very well in the service.

If you really need to set the timer interval to more than 2 ^ 31 milliseconds (i.e. you want your program to do something once every 30 days), you can write a managed wrapper in the thread pool timer API. It allows you to specify a timer interval of up to 2 ^ 63 100-nanosecond ticks (approximately 2 ^ 50 milliseconds, or about 30,000 years). See CreateThreadPoolTimer and related functions.

+2


source







All Articles