Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer control unpredictable- someone with timer experience please read

Status
Not open for further replies.

Devin

Programmer
Dec 17, 2001
42
US
Thank you very much for any advice you can give me. I've been pulling my hair out all morning over this.

I am observing some very confusing behavior from my timer controls.

Let's say I have a timer with an interval of 20ms and it is always enabled. The code associated with the timer event takes 5ms to execute. Will that code be executed every 20ms or every 25ms? In other words, does the timer continute to run while it's event is being executed? [I would hope yes] (This is a type of data acq. application, so I need exact timing.) Can anything else pause or slow a timer?

A related question concerns re-trigger-ability. Let's say I have the same timer but that its event code takes 50ms to execute. Does the event get triggered again 20ms into execution, thus eventually overflowing the stack? Or, are subsequent events ignored until execution is complete? If they are ignored, then when event execution finishes, and a trigger has been missed, does it execute the event right away or does it wait until the timer triggers again?

If you read the help file, you get the idea that timer truly generates an event at an exact regular interval. However, in my program, I never get as many total events out of my timer as I expect, even if I lengthen the interval so that it is much longer than the time required to execute the event code.
 
Yes, the timer run continuously, so in your example, the second invocation will begin 15ms after the termination of the 5ms code on your 20ms timer.

Yes, you can disable the timer and re-enable it at will.

Sub tmrTimer_Timer

tmrTimer.Enabled = False ' Timer is Disabled

<do your code>

tmrTimer.Enabled = True

End Sub

this will cause the second iteration to run 20ms after completion of the code.

You can also programatically change the interval by adjusting the Interval property at run time. These properties/methods may be referenced anywhere within the form.

Good Luck

 
The answer I fear is not. It won't restart until your code is done.

You could get a 3rd party timer, or use an infinite loop doing a check on the actual time value. You can basically make your own programatic timer, though it has overhead.
[tt]
'Global Declarations
lgMilliSeconds as Long
dtLastRun as Date
blDoLoop as Boolean

Private Sub EndLessLoop()
Do While blDoLoop = True
Do Events
If dtLastRun >= Now() Then
dtLastRun = DateAdd(&quot;m&quot;, Now(), lgMilliSeconds)
Call CodeFunction
End If
Loop
End Sub

Private Sub cmdStop_Click()
blDoLoop = False
End Sub

Private Sub cmdStart_Click()
blDoLoop = True
Call EndLessLoop
End Sub
Craig, mailto:sander@cogeco.ca

&quot;Procrastination is the art of keeping up with yesterday.&quot;

I hope my post was helpful!!!
 
Cajun I could never get the Timer to work that way. It always waits until my code is done before it start it's next iteration. Craig, mailto:sander@cogeco.ca

&quot;Procrastination is the art of keeping up with yesterday.&quot;

I hope my post was helpful!!!
 
You may be right Craig, but I did experience the &quot;overflow&quot; condition a few years ago, and so I always disabled the timer, exec my code, then reenable the timer, and I've done it that way ever since. Maybe things have changed.
 
I actually just tested it and the timer does wait until code is executed before it goes on to time the next event.

From MSDN:
The Interval property has a few limitations to consider when you're programming a timer control:

If your application or another application is making heavy demands on the system — such as long loops, intensive calculations, or drive, network, or port access — your application may not get timer events as often as the Interval property specifies.

The interval can be between 0 and 64,767, inclusive, which means that even the longest interval can't be much longer than one minute (about 64.8 seconds).

The interval is not guaranteed to elapse exactly on time. To ensure accuracy, the timer should check the system clock when it needs to, rather than try to keep track of accumulated time internally.

The system generates 18 clock ticks per second — so even though the Interval property is measured in milliseconds, the true precision of an interval is no more than one-eighteenth of a second.

Devin If you find a 3rd Party control that is more accurate let us know. It may be usefull for others.
Craig, mailto:sander@cogeco.ca

&quot;Procrastination is the art of keeping up with yesterday.&quot;

I hope my post was helpful!!!
 
My recent experients seem to confirm that the timer does not count down while it's executing its event, even if you never disable it. I guess the lesson here is that, for this kind of measurement and control application, timer is useless, b/c you can't reliably predict execution time. Apparently, the only way to do what I want is to make use of the system clock.
 
in my earlier example the date add function was a little out of whack, &quot;s&quot; is for second, and the interval is next. Craig, mailto:sander@cogeco.ca

&quot;Procrastination is the art of keeping up with yesterday.&quot;

I hope my post was helpful!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top