0

i use this code:-

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If button.Location.Y >= 618 Then
    MessageBox.Show("You lost!", "Failure", MessageBoxButtons.RetryCancel)
    Timer1.Enabled = False
    End If
    End Sub

Let us assume the condition is always true then instead of disabling timer1, it display messagebox infinite times.But if i write Timer.enable = false first and then messagebox then the timer stops. Why does this happen

Sniper
  • 1,428
  • 1
  • 12
  • 28
  • 1
    When you display a message box with show, the code after it won't execute until the message box is closed. Time1.Enabled = False will only be executed after the message box is closed. – the_lotus Mar 04 '15 at 16:53
  • 2
    The name of the "defendant" is: [Application.DoEvents](http://stackoverflow.com/questions/5181777/use-of-application-doevents) Also related: [What is a message pump?](http://stackoverflow.com/questions/2222365/what-is-a-message-pump) – Bjørn-Roger Kringsjå Mar 04 '15 at 17:10

1 Answers1

1

Because the tick is firing in a round about Async manner. Multiple tick executions can be occurring at the same time if a previous one hasn't completed (and it hasn't in your case because it's waiting on the MessageBox to be clicked away). That means while the message box is waiting for the "Ok" other tick events are firing (because you haven't got to the disabled part yet).

You probably would want to put the Timer1.Enabled = false before the MessageBox although that could still potentially run into a race condition though less likely, you'd want to test that.

b.pell
  • 3,873
  • 2
  • 28
  • 39