|
-
Apr 30th, 2013, 09:57 AM
#1
Thread Starter
Junior Member
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!
-
Apr 30th, 2013, 10:25 AM
#2
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
-
Apr 30th, 2013, 10:59 AM
#3
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
 
-
Apr 30th, 2013, 12:27 PM
#4
Thread Starter
Junior Member
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...
-
Apr 30th, 2013, 12:29 PM
#5
Thread Starter
Junior Member
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
-
Apr 30th, 2013, 12:30 PM
#6
Re: how to stop a timer inside a timer
Before
Me.Timer1.Stop()
put
Stop
I'll bet that the If is never true.
-
Apr 30th, 2013, 12:33 PM
#7
Thread Starter
Junior Member
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!!!"
-
Apr 30th, 2013, 12:33 PM
#8
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!
-
Apr 30th, 2013, 12:38 PM
#9
Thread Starter
Junior Member
Re: how to stop a timer inside a timer
dunfiddlin you are my hero!!!!!!!!!!!!! 
-
Apr 30th, 2013, 09:20 PM
#10
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:
'
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Prevents the Tick events from being
'processed after the timer was stopped
If Not Timer1.Enabled Then Return
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
-
Apr 30th, 2013, 10:04 PM
#11
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.
-
May 1st, 2013, 11:22 AM
#12
Thread Starter
Junior Member
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...
-
May 1st, 2013, 11:38 AM
#13
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
 
-
May 1st, 2013, 07:46 PM
#14
Re: how to stop a timer inside a timer
 Originally Posted by JaneTheSardine
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.
-
May 1st, 2013, 09:03 PM
#15
Re: how to stop a timer inside a timer
 Originally Posted by JaneTheSardine
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|