newbie timer question

  • Thread starter Thread starter Chris Winstanley
  • Start date Start date
C

Chris Winstanley

Hi,

I'm running version 1.0 of the .net framework on windows XP and Windows
2000.

I need to run a timer that fires accurately at a 10ms interval. I've
been having problems trying to get a System.Threading.Timer to run
accurately at this rate (it seems to wander around 12ms to 20ms). The
code running on the timer takes only 3ms to execute. All measurements
have been done using QueryPerformanceTimer.

I've also noted that there doesn't appear to be any way to set the
thread priority of a timer.

At the moment I'm considering ditching the timer and simply doing a busy
wait using QueryPerformanceTimer to give me the accuracy I require.

Surely there is a better way to achieve the accuracy I require?

One more question: If the System.Threading.Timer does overrun it appears
that another instance of the timer is created and run concurrently. This
is fine until you attempt to dispose of the timer. The timer is disposed
in one instance, another instance fires and exceptions because the timer
handle is now null. Is there anyway to ensure all the timer instances
are disposed of in one go?

Thanks,
Chris Winstanley
 
The timer is not precise. If You set time to period to 12 ms, it is not
guarantee that You receive timer event after 12 ms. If the operating system
is busy, You can receive this event even after few seconds. Maybe, better
way is System.Threading.Thread.Sleep(time_period).
 
Hi Chris

Unfortunately Microsoft does not make real-time operating systems. You
cannot guarantee timer accurracy.

For instance, under DOS and Windows 3.1 the hardware timer ticks were at
approximately 18 a second (A day divided by 2^16 if I remember correctly).
It was quite common to get no events for up to a second, then a single timer
event. This was by design - timeouts are generally low priority for business
applications.

You may be able to improve your program by asking for the system time on
each timer event. You will then have some idea of how long it was since the
last time the timer was serviced. If that is no help, then you are into the
murky world of writing a device driver (I've managed to avoid that myself).

Regards

Ron
 
Back
Top