Timers problem

juhah

Active member
Joined
Apr 16, 2007
Messages
28
Programming Experience
3-5
Here's my problem:

I set a timer interval as 200 ms. If there is heavy code in the tick event, does the timer work like this?:

- timer interval is 200 ms
- it takes 20 ms to run the code in the tick event
- after tick event the timer waits 200 ms again so after all it has taken 420 ms to run one event?

There are problems in the timer too. For example if the interval is 346 ms, the real time can be 353 ms. I calculated the real time with simple code but I think it shouldn't take 7 ms to calculate the time difference... How can I make really accurate timer?

How does the timer tick event really work? The event is in the same class as the measurement code. Does the tick event code prevent all other procedure of code or does it work like a thread?

I need very accurate timer since I'm doing a data measurement with quite short intervals
 
The Timers in VB.Net may not be what you need. But in answer to your questions...

How does the timer tick event really work? The event is in the same class as the measurement code. Does the tick event code prevent all other procedure of code or does it work like a thread?

It works like a thread.

I set a timer interval as 200 ms. If there is heavy code in the tick event, does the timer work like this?:

- timer interval is 200 ms
- it takes 20 ms to run the code in the tick event
- after tick event the timer waits 200 ms again so after all it has taken 420 ms to run one event?

Effectively, Yes.

There are problems in the timer too. For example if the interval is 346 ms, the real time can be 353 ms. I calculated the real time with simple code but I think it shouldn't take 7 ms to calculate the time difference... How can I make really accurate timer?

The stopwatch class in vb.net 2005 wraps the QueryPerformanceCounter API call in Kernal32.dll. It has a resolution of 0.000001 and should be more than adequate for your needs. However since you are using .net 1.1 you will have to do the call your self with a Plaform Invoke (PInvoke) call.

EDIT: However, with the marshalling that will take place when the PInvoke call is made you made just lose some accuracy. How much I don't know. You can speed up any PInvoke by 'telling' the CLR to skip the stack walk that is made when making calls into unmanaged code. You'll need the following attribute. <System.Security.SuppressUmanagedCodeSecurity>
 
Timer of System.Windows.Form can be inaccurate to 55ms it says. You can try Timer of System.Timers. (You can't use StopWatch also because it doesn't have events.)
 
According to documentation the System.Timers.Timer has approximately the same level of accuracy as a System.Windows.Forms.Timer. The only 'real' difference between the two is that a timer from System.Timers uses thread from the threadpool and has a syncronizingobject member allowing merging of the thread back into the main UI thread to safely access visual components created on the main UI thread.

That doens't mean to say that it is not atleast worth a try though.
 
The Timers in VB.Net may not be what you need. But in answer to your questions...



It works like a thread.



Effectively, Yes.



The stopwatch class in vb.net 2005 wraps the QueryPerformanceCounter API call in Kernal32.dll. It has a resolution of 0.000001 and should be more than adequate for your needs. However since you are using .net 1.1 you will have to do the call your self with a Plaform Invoke (PInvoke) call.

EDIT: However, with the marshalling that will take place when the PInvoke call is made you made just lose some accuracy. How much I don't know. You can speed up any PInvoke by 'telling' the CLR to skip the stack walk that is made when making calls into unmanaged code. You'll need the following attribute. <System.Security.SuppressUmanagedCodeSecurity>


Thanks for your answer. This sounds something I could use, it's just I don't know how to do it. Could you post a small example code of using that API call? I'm not familiar using those..
 
Learned something new here.. Someone, as always, has beaten me to the puch and already created on. See the link here

It makes use of the Multimedia timers in Windows. Currently there is no wrapper for these in the .net framework according the article anyway. Check it out for yourself. It includes source and a demo project.
 
According to documentation the System.Timers.Timer has approximately the same level of accuracy as a System.Windows.Forms.Timer.
The documentation link I posted says it has greater accuracy, I can't find anything about its approximation, though.
 
Learned something new here.. Someone, as always, has beaten me to the puch and already created on. See the link here

It makes use of the Multimedia timers in Windows. Currently there is no wrapper for these in the .net framework according the article anyway. Check it out for yourself. It includes source and a demo project.


This would be a good solution. But it's written in C#. I tried to re-write it in VB.NET but it'sa bit hard though. I wonder if someone has done the same with VB..?
 
You don't have to convert it. If you place the source in a class library project and build it into a .dll you can then reference it from vb.net
 
You don't have to convert it. If you place the source in a class library project and build it into a .dll you can then reference it from vb.net

Ok I got it working. But it's not accurate. If i put the interval as 439, the real interval varies +/- 4 ms. On the other hand an average value of 10 intervals is very close to 439. How could I get a timer that has every interval precisely 439 ms?
 
You are asking a lot unless you are prepared to pay for a component that may do the job, I'm not even sure if it's possible. Thing is that this is all dependant on what other things the processor maybe doing at the time. To get it down to a millisecond every time i doubt is possible in vb.net. You'd need to move over to unmanaged code where you have more control over what is going on, other than that you may well have to buy one. They are not expensive, here's one
 
Back
Top