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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about timers...

Status
Not open for further replies.

OrthoDocSoft

Programmer
May 7, 2004
291
US
Folks,

If I have a timer and it performs a process, and that process sometimes might take longer to perform than the timer interval, will the timer execute again "on time" before the first timer execution has finished, thus creating an "overlapping" code execution?

For example: (timer1.interval = 10000, or 10 seconds)

Code:
public timer1_timer

   call RaiseAFlagAndWaveItFor20Seconds

end

I don't think so, and I hope not, but I thought I would ask.

I want it to be so that if the process triggered by the timer, in this case, waving a flag for 20 seconds (twice as long as the intended timer interval), that the timer interval (10 secs) will be overridden, that only one instance of flag waving will occur at a time, and that the flag waving process will come to an end BEFORE the next instance of flag waving is STARTED by the timer.

Would someone who knows please confirm this for me?

Thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Code:
public timer1_timer
   Static bEscape as boolean
   'Get out if process is still running
   If bEscape=True then Exit Sub
   bEscape = True

   On Error Goto errh
   call RaiseAFlagAndWaveItFor20Seconds
errh:
   bEscape =false
end
 
Yes, SBerthold, that is something like what I have been doing, so that tells me that you think the timer can be "called again on top of itself" yes?

[lookaround] "you cain't fix 'stupid'...
 

Ah yes, you are using a VB Timer control.

Well, using the VB Timer, I do not know, but I guess it wouldn't fire again. However I wouldn't always trust it not to do so. Maybe I am wrong.
I do not use the VB Timer at all, but use a different timer, and it's event can fire again, especially when time is given back to the system with something like a DoEvents.

For the VB Timer, a simpler method may be to just disable the timer within the procedure:

public timer1_timer

Timer1.Enabled = False
'Do Something
Timer1.Enabled = True

End Sub
 
>I want it to be so that if the process triggered by the timer, in this case, waving a flag for 20 seconds (twice as long as the intended timer interval), that the timer interval (10 secs) will be overridden


It appears that your desired effect is what happens.

to test add 2 timers and a label, set timer1 interval to 1.

as you will see the label fires once every second, thus timer2 process is overriding timer1 process.

if this were not the case label1 should update every millisecond

Code:
Dim X As Long
Private Sub Timer1_Timer()
    Timer2.Interval = 1000
End Sub

Private Sub Timer2_Timer()
    X = X + 1
    Label1.Caption = X
End Sub
[morning]
 
>if this were not the case label1 should update every millisecond

Barring the fact that VB's Timer control is not actually accurate to 1ms (normally it'll be 10 to 15ms depending on your OS), on what do you base this assertion?
 
>will the timer execute again "on time" before the first timer execution has finished

No
 
if you are not running some magic external process then your timer will wait for everything to complete before carrying on.

Here's proof: set timer1 to interval 1 (THAT IS FAST). I count the ticks since timer1 was started, and in between each tick i make the program pause for 3 seconds:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim x As Integer

Private Sub Form_Load()
x = 1
End Sub

Private Sub Timer1_Timer()
x = x + 1
Me.Caption = x

Sleep 3000
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top