threading timers fires few seconds before the actual due time RRS feed

  • Question

  • Hi,

    We're getting following problem while using system.threading.timer ( .net framework 2.0 ) from a Windows service.

    There are around 12 different timer objects..
    Each timer has due time and interval. This is set correctly.
    It is observed that after 3 to 4 hours, the timers start signalling before their interval elapses. For example if the timer is supposed to signal at 4:59:59, it gets signalled at 4:59:52, 7 seconds earlier.
    Can someone tell me what is the cause for this behavior and what is the solution for that ?

    Thanks, Gayatri

    Friday, March 12, 2010 1:02 PM


All replies

  • Here is some explanation:

    All timers in the Framework are using the System clock as timer source, this clock has a (system wide default) resolution of x milliseconds (where x is 10 msec. on most X86 based hardware) - that means that the clock changes (fires) every x (10) msec.
    Say you initialize a timer to fire after 10 seconds, and say this happens 2 msec. after the last system clock tick, that means that the timer counts 10000 ticks before it fires, that is after 8 msec. (to the first tick) + 9999 * 10 msec = 9999.8 msec.


    Friday, March 12, 2010 5:18 PM
  • If you need events to happen by the real-time clock (as opposed to the system clock), you will have to kick your timers on some reasonable period (resolution depends on how close to the real-time clock you need to be), then check the real-time clock to see if the time has come up.

    In your case this will provide an additional advantage because you can get rid of 11 timers. You only need one.
    Friday, March 12, 2010 9:13 PM