Results 1 to 15 of 15

Thread: how to stop a timer inside a timer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    how to stop a timer inside a timer

    I have a program where I have to stop my timer inside the same timer, but it isn't working. I tried calling a sub to stop it, but that didn't work either. Any help is appreciated!

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: how to stop a timer inside a timer

    Seems to work to me.

    Code:
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Timer1.Interval = 100
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Stop()
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: how to stop a timer inside a timer

    I suspect that what you are trying to do is more complicated than what you described, as what you described is really quite common (and is what dbasnett showed).
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: how to stop a timer inside a timer

    i have the same type of code and same "timer1.stop" as he put, and it is inside the timer, but the timer keeps on going for another like 40-50 ticks, then quits...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: how to stop a timer inside a timer

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    For f As Integer = 0 To hitx.Length - 1
    Dim pixelcolor As Color = bmp.GetPixel(hitx(f), hity(f))
    If pixelcolor.Name = "ff008000" Then
    MessageBox.Show("YOU CRASHED!!! GO BACK TO DRIVING SCHOOL!!!")
    Me.Timer1.Stop()
    Exit For
    End If
    Next
    End sub

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: how to stop a timer inside a timer

    Before

    Me.Timer1.Stop()

    put

    Stop

    I'll bet that the If is never true.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: how to stop a timer inside a timer

    No, the if is true. because i end up with 50 messageboxes saying "YOU CRASHED!!! GO BACK TO DRIVING SCHOOL!!!"

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: how to stop a timer inside a timer

    What interval are you using? Too short and you get unpredictable behaviour!

    But the main problem is the MessageBox. The Timer stop isn't executed until you close the box by which time any number of new tick events could be queued!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: how to stop a timer inside a timer

    dunfiddlin you are my hero!!!!!!!!!!!!!

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: how to stop a timer inside a timer

    I first encountered this problem with timers back in VB6. Its pretty easy to solve:-
    vbnet Code:
    1. '
    2.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    3.         'Prevents the Tick events from being
    4.         'processed after the timer was stopped
    5.         If Not Timer1.Enabled Then Return
    6.  
    7.         For f As Integer = 0 To hitx.Length - 1
    8.             Dim pixelcolor As Color = bmp.GetPixel(hitx(f), hity(f))
    9.             If pixelcolor.Name = "ff008000" Then
    10.                 MessageBox.Show("YOU CRASHED!!! GO BACK TO DRIVING SCHOOL!!!")
    11.                 Me.Timer1.Stop()
    12.                 Exit For
    13.             End If
    14.         Next
    15.     End Sub
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to stop a timer inside a timer

    By the way, there is nothing here that is "inside a Timer". All your code is in the Tick event handler of the Timer, which is not inside the Timer. It is inside the form. If you open the code for the form then all the code you see is inside the form. The event handlers for Button Clicks, Timer Ticks, etc, are all part of the form, not those individual controls and components.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: how to stop a timer inside a timer

    But clearly everyone still understood exactly what I meant enough without being gramatically correct or whatever you wish to call that...

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: how to stop a timer inside a timer

    Yeah, we're getting used to reading between the lines. It wasn't technically correct, and I'm not one to be able to say anything about gramar.

    The actual problem was one of some interest. As you may or may not have realized by now, when the timer ticks, it doesn't really do much. All that happens is that the tick is put onto the message queue. Once the program gets around to popping stuff off the queue, that's when the tick handler is called, but lots of ticks can stack up on the queue if the tick is happening fast enough, or if other processes are blocking message pumping. Where this gets particularly interesting is when the activity in the Tick handler takes longer than the tick interval. You kind of did that in your case with the messagebox, but you can never tell how long a messagebox will be open, so it doesn't really count. If you were doing some onerous calculation in that tick event that, itself, took longer than the interval...just think what could be happening to the message queue. I suspect that it might eventually crash, but the behavior could sure get weird before that.
    My usual boring signature: Nothing

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to stop a timer inside a timer

    Quote Originally Posted by JaneTheSardine View Post
    But clearly everyone still understood exactly what I meant enough without being gramatically correct or whatever you wish to call that...
    I apologise profusely for trying to help you understand the way things actually work. Clearly, all that matters is that a rough approximation of the actual meaning is conveyed and trying to actually do things correctly is just not worth the effort. While it may not be your intention to work in the software development industry, if it is then good luck getting a job with that attitude.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: how to stop a timer inside a timer

    Quote Originally Posted by JaneTheSardine View Post
    But clearly everyone still understood exactly what I meant enough without being gramatically correct or whatever you wish to call that...
    Actually, your OP made no sense. I didn't know what you were talking about until dunfiddlin's post.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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
  •  



Click Here to Expand Forum to Full Width