Timer Control Not Turning Off
I have Timer1 with code in it. I've set the Timer in motion but then later set it's Interval to 0, yet it continues. It doesn't just continue one extra time, it continues... well, continuously. Isn't setting the Interval to 0 supposed to stop it? I've had problems in the past with setting a Timer.Enabled = False and it not turning off, but never with using the .Interval. Also, what's the difference between disabling a Timer and setting it's Interval to 0?
Re: Timer Control Not Turning Off
Maybe the Interval is changed somewhere else after setting to 0, e.g. another Timer procedure.
Re: Timer Control Not Turning Off
You should show your code so we don't have to second guess what's going on - chances are it's code issue and not the Timer control
Re: Timer Control Not Turning Off
I've never tried setting the interval to 0. I always use the enabled property and have not once saw a problem dating all the way back to before VB6 was released.
Most likely you have something else in your code that is causing your problem as was probably the case when you had problems with enabled=false not working properly. I've written hundreds of apps that use a timer control and have never saw an instance where it did not start and stop as expected.
Re: Timer Control Not Turning Off
never had an issue with setting the enabled property to false to stop a timer... that said, it should be the FIRST thing the code in the Tick event does ... disable the timer, then you re-enable it just before exiting... prevents the event from firing off again while you're still processing the current tick event.
-tg
Re: Timer Control Not Turning Off
There is a known issue with Timer control when it continues to work after unloading a form under some conditions, here is a workaround: http://stackoverflow.com/questions/9...answer-9573927
Re: Timer Control Not Turning Off
Timer event is not re-entrant while processing in the Timer event
Re: Timer Control Not Turning Off
Quote:
Originally Posted by
jmsrickland
Timer event is not re-entrant while processing in the Timer event
True, unless you or something you call from there calls the DoEvents() function.
I'm pretty sure the WM_TIMER events continue to get posted to your message queue as well, and though they get discarded you burn a few more cycles. But then I can't recall exactly how the VB6 Timer was implemented anyway and it may use a callback instead of window messages.
Re: Timer Control Not Turning Off
Quote:
Isn't setting the Interval to 0 supposed to stop it?
Yes it is.
Quote:
Also, what's the difference between disabling a Timer and setting it's Interval to 0?
I don't know of any.
Sometimes a typo like Timer2.Interval = 0 instead of Timer1.Interval = 0 is a culprit in what you are describing (Where Timer1 is the one you want to 'disable'). Like it was said earlier, check your code elsewhere...both procedures for 'stopping' a Timer are acceptable and I also have never experienced an issue with doing it either way (although I normally use the .Enabled property). The only time I might use the Interval might be if I were passing different values around, starting and stopping the time based upon those values, then it might be 'logical' to use 0.
Re: Timer Control Not Turning Off
Quote:
Originally Posted by
dilettante
True, unless you or something you call from there calls the DoEvents() function.
I would think that 're-entrant' implies re entering a sub at the beginning of the sub as is multi threading where a sub can be entered from more than one point and have several processings taken place at the same time. When I said not re-entrant I mean not from the sub's main entry point. Of course, calling DoEvents will cause it to exit but return at the point following the DoEvents statement so it won't mess up any thing that has already taken place during the processing in the event.
Re: Timer Control Not Turning Off
The timer event will not fire again until the timer event has completed even if you have a loop with doevents in te timer or call a routine that does so.
Code:
Private Sub Timer1_Timer()
Debug.Print "Timer Fire"
Dim X As Long
For X = 1 To 1000000
If X Mod 100000 = 0 Then
Debug.Print X
Pause 5
End If
DoEvents
Next
Debug.Print "Loop Complete"
End Sub
Private Sub Pause(HowLong As Integer)
Dim Start As Long
Start = Timer
Do While Timer < Start + HowLong
DoEvents
Loop
End Sub
Output is
Quote:
Timer Fire
100000
200000
300000
400000
500000
600000
700000
800000
900000
1000000
Loop Complete
Timer Fire
100000
As you can see even with the 5 seconds of doevents every 100,000 loops it did not fire the timer event until the event had completed.
Timer interval was set to 1 for this test
Now of course this was in the IDE. It may behave differently as an exe where the timer runs on its own thread.
Re: Timer Control Not Turning Off
Quote:
Originally Posted by
techgnome
never had an issue with setting the enabled property to false to stop a timer... that said, it should be the FIRST thing the code in the Tick event does ... disable the timer, then you re-enable it just before exiting... prevents the event from firing off again while you're still processing the current tick event.
-tg
Although not necessary I tend to agree with tg post 5.....
Code:
Private Sub Timer1_Timer()
Timer1.Enabled = False '<---- do it here so you won't forget to do it later
'
'
' Do stuff
'
'
Timer1.Enabled = True '<---- can be done elsewhere
End Sub
.....this way you won't forget to do it later in your code which can lead to undesirable consequences.