dcsimg
Results 1 to 12 of 12

Thread: Timer Control Not Turning Off

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Question 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?

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,556

    Re: Timer Control Not Turning Off

    Maybe the Interval is changed somewhere else after setting to 0, e.g. another Timer procedure.



  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    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


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    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.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,381

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    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

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Timer Control Not Turning Off

    Timer event is not re-entrant while processing in the Timer event


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,322

    Re: Timer Control Not Turning Off

    Quote Originally Posted by jmsrickland View Post
    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.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,011

    Re: Timer Control Not Turning Off

    Isn't setting the Interval to 0 supposed to stop it?
    Yes it is.
    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.

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Timer Control Not Turning Off

    Quote Originally Posted by dilettante View Post
    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.
    Last edited by jmsrickland; Jul 20th, 2015 at 04:05 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    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
    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.

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Timer Control Not Turning Off

    Quote Originally Posted by techgnome View Post
    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.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width