"LJJ" <anonymous@discussions.microsoft.com> wrote in
news:3F654DB7-73FF-46D3-848E-9DB0F9907C6F@microsoft.com...
[quoted text, click to view] > Hi:
> I have noticed that the time resolution of System.Timers.Timer and
System.Threading.Thread.Sleep() varies significantly between systems of
seemingly similar class.
[quoted text, click to view] >
> For example on my HP Centrino 1.6GHz laptop, I see a resolution of 10ms
for the event from System.Timers.Timer and a delay in
System.Threading.Thread.Sleep() - e.g. if I change the delay in the latter
(in a "highest priority" thread) from 30 ms to 20 ms, I see a change in the
delay (although changing from say 30ms to 22ms makes no difference).
[quoted text, click to view] >
> On the other hand, on a 2.0 GHz P4 desktop machine, I see a resolution in
these same instances of more like 30ms i..e changing from 30ms to 20ms (or
even to 15ms) leaves the delay at essentially 30ms.
In my experience a Centrino 1.6 is the same class as a 3.0 GHz P4...
Anyway: Windows is a preemptive multitasking OS, that is some thread
executes, and from time to time (when a timer interrupt comes in, or when
the active thread ceases control) the task scheduler checks if it's some
other thread's turn to work.
Now, the Sleep routine effectively tells the task scheduler to "ignore" the
current thread for a specified number of timer-interrupts, yielding its
CPU-time to other threads.
So, since the thread-scheduler will usually be invoked every 10 ms or so
(personal experience) Sleep will usually have a resolution of 10 ms.
[quoted text, click to view] > In both of these cases, the System.Environment.TickCount (which I am using
to instrument these cases) seems to have a resolution of 1ms or better.
I think that "DateTime.Now.Ticks" is the most precise timer available in
..net;
Pentium (and higher) processors have an internal clock-tick-counter, (I'm
not 100% sure of the bus or the processor determines its frequency, but it
will usually be > 30 MHz). This value is available through the
QueryPerformanceCounter API. I think DateTime.Now.Ticks internally uses this
API (but again, I'm not 100% sure).
[quoted text, click to view] > Can anyone tell me where the effective resolution of these timers is
obtained on a given system - and also whether there is a way to improve the
resolution, as my current project requires timing incoming data with time
intervals of the order of 30 ms. I can (and do) keep track of the total
number of passes vs. the expected number of passes and make appropriate
corrections, but having a higher resolution timer would make things easier.
The standard response to that kind of problem is: Windows is no realtime-os;
You can never guarantee how long some operation (response time, delay...)
takes.
That said: the Tick-Counters mentioned above are quite precise and reliable;
Sleep&friends weren't designed to be precise: they were desinged to be
cooperative; I'm not sure if you can change the task-scheduler frequency,
maybe you should ask that in some kernel ng.
Where does the "incoming data" come from?
Do you know there's no other delay (e.g. some hardware-buffer)?
Niki