blah blah blah is here! blah blah » Close

up1down
link

Dear All,

Anybody knows how much is the accuracy of the timer component in the C# toolbox? It says it is 1ms. From what I did is I am using 2 timers. Timer1 and Timer2. Timer1 is 1ms each tick, and Timer2 is 1000ms or 1 sec. Each tick of Timer1 keep on adding a +1 to a variable in which during the Timer2 tick, the value of the variable will be shown.

The surprising thing is the value of the variable is far away from what i expected. I would expect 1000 for the value, and the test returns 63. So it is around 16-17ms each tick of timer1. This is rather surprising for me.

My question is, is what I am doing right? That the timer1 properties lie about the accuracy until 1ms? And can I use C# for making something with 1ms or 2ms accuracy?

Thank you in advance.

Kindest Regards,




E

last answered one year ago

2 answers

up1down
link

The accuracy of the System.Windows.Forms.Timer is only about 55 ms.

The System.Threading.Timer and the System.Timers.Timer are more accurate though precision is still limited to (at best) 10 ms.

To get down to 1 ms accuracy, I'd check out the Windows multimedia timer.

To my knowledge, there's no .NET wrapper for this and so you have to P/Invoke the appropriate unmanaged functions from winmm.dll. However, there's a good example of usage in this thread.

Do you have any links about the multimedia timer? I really need to tick every 3ms :)

vulpes
17279

For a 3 ms delay, set the first parameter to timeSetEvent to 3 and the (only) parameter to both timeBeginPeriod and timeEndPeriod to 3. The C/C++ documentation for timeSetEvent can be found at http://msdn.microsoft.com/en-us/library/dd757634(VS.85).aspx with links to the other functions in the treeview on the left. Don't worry about the fact that the timeSetEvent function is described as obsolete - it still works and is far easier to use from C# than CreateTimerQueueTimer :)

up0down
link

just wanted to add:
the timer depend on crystal used in your pc. so they will never be exactly same everywhere

cpo

Feedback