I have a windows app which is just a form with a timer control on. I’ve managed to track this down to the following situation:
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("Test");
timer1.Enabled = false;
}
Will print Test again and again until I stop the program. However:
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("Test");
textBox1.Text += "t";
timer1.Enabled = false;
}
Just adds a single “t” to the textbox.
Can anyone tell me why MessageBox.Show is causing the function to return before the timer is disabled?
The call to
MessageBox.Showblocks execution oftimer1_Tickuntil you close the messsagebox, so the call to settimer1.Enabled = false;doesn’t occur until after that. Because of this, the timer is still running and thustimer_Tick` still keeps getting called, every time the timer fires, until you hit OK on one of the message boxes.What you need, if you want displaying the messagebox to stop the timer from firing again, is: