DoEvents doesn't always work
Hello everybody. I used the following code to try the DoEvents function. On a form put Timer1 with Interval=1000 and Timer2 with Interval=5000:
Code:
Dim t As Integer
Private Sub Timer1_Timer()
t=t+1
End Sub
Private Sub Timer2_Timer()
Static i as Integer
i=i+1
Debug.Print i
t=0
Do Until t=10
DoEvents
Loop
i=i-1
End Sub
If you launch the code, you'll see that i always equals 1. But it shouldn't be so, because of the DoEvents, which should allow the Timer2_Timer event to fire while another Timer2_Timer() Sub is being executed. Do you know why this is so? Thanks.
Re: DoEvents doesn't always work
You have a logic error here: Timer2_Timer event is never fired more than once, there will never be two Timer2 events running at the same time. Thus as you enter the sub you set i to 1 and then when you exit it is set back to 0.
Also, I don't think this kind of coding would give any real benefit (but I'm generally not a fan of DoEvents anyway).
Re: DoEvents doesn't always work
Quote:
Originally Posted by
Merri
You have a logic error here: Timer2_Timer event is never fired more than once, there will never be two Timer2 events running at the same time. Thus as you enter the sub you set i to 1 and then when you exit it is set back to 0.
Also, I don't think this kind of coding would give any real benefit (but I'm generally not a fan of DoEvents anyway).
This has to be a by-design. They designed the Timer event in such a way that it can only fire after the preceding one's sub has been executed. Isn't it?
Re: DoEvents doesn't always work
I am 98% sure the Timer control is a repeating one-shot timer, not a simulated real-time clock interrupt. So what I'm saying is that I strongly suspect (but haven't proven) that the Timer is re-armed only when you exit its event handler.
This may explain the wailing and gnashing of teeth resulting in people saying Timer controls are "not accurate." They may be confusing an apple with a banana.