Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application, I have used the number of System.Threading.Timer and set this timer to fire every 1 second. My application execute the thread at every 1 second but it execution of the millisecond is different. My output is given below:
10:28:32.875
10:28:33.390
10:28:34.875
....
10:28:39.530
10:28:40.875

However, I want following results:
10:28:32.000
10:28:33.000
10:28:34.000
....
10:28:39.000
10:28:40.000

How can the timer be set so the callback is executed at "000 milliseconds"?
Posted

I think it is almost impossible to get the timer tick event to fire exactly after 1 sec. The reason behind it is, windows is not a realtime operating system. And Timers are by nature inexact when it comes to multithreaded operating system like windows.

Let me explain you.
e.g At the very first start Timer firs exactly after 1 sec, now when it comes to firing the event second time, suppose operating system is busy executing something else, then obviously it will take some time to switch from that process to the timer process. In this scenario say 10 ms are passed. So obviously your timer will fire 10 ms late.

But it is not totally impossible to get the accuracy. You can have some hardware arrangement to take of the timer event.

Hope this helps.
All the best.
 
Share this answer
 
Timers only give you elapsed times and at best to a 15ms resolution.

So a 1 sec timer event is 1 sec +- 15ms every second.

You can always truncate the milliseconds and not show them.
 
Share this answer
 
Comments
Mehdi Gholam 28-Sep-11 2:46am    
An example of what?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900