Results 1 to 4 of 4

Thread: timer event is not considered

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    77

    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

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

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Do
    3.             If timer_event Then
    4.                 MsgBox(timer_event)
    5.                 timer_event = False
    6.                 Timer1.Start()
    7.             End If
    8.             My.Application.DoEvents()
    9.         Loop
    10.  
    11.     End Sub



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

    Re: timer event is not considered

    You shouldn't have that Do loop in there at all. Get rid of it.
    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

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

    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
  •  



Click Here to Expand Forum to Full Width