|
-
Nov 18th, 2011, 03:19 AM
#1
Thread Starter
Lively Member
timer event is not considered
Hi,
can anyone help me understand why after you pressed the button Button1, this application crashes without considering the event timer1??
Code:
Public Class Form1
Public timer_event As Boolean
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
timer_event = False
Timer1.Interval = 3000 '3 sec
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
timer_event = True
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Do
If timer_event Then
MsgBox(timer_event)
timer_event = False
Timer1.Start()
End If
Loop
End Sub
End Class
-
Nov 18th, 2011, 03:49 AM
#2
Re: timer event is not considered
It is not crash, it simply enter an infinite loop, you have to insert DoEvents inside the loop to enable other events fire, Try this
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Do If timer_event Then MsgBox(timer_event) timer_event = False Timer1.Start() End If My.Application.DoEvents() Loop End Sub
-
Nov 18th, 2011, 04:07 AM
#3
Re: timer event is not considered
You shouldn't have that Do loop in there at all. Get rid of it.
-
Nov 18th, 2011, 10:49 AM
#4
Re: timer event is not considered
I'd go further than that. You have a timer raising an event when time expires. The only possible purpose for the loop is that you are waiting for that tick event. What's the point of having code running waiting for the event? You are using a flag to signal a different method when the timer ticks, which is wildly inefficient. Just move the code into the tick event itself and get rid of all that is happening in that button click. Let the raising of the event be its own signal.
My usual boring signature: Nothing
 
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
|