Search code examples
c#if-statementtimermessagebox

All items in if statement not firing


I'm trying to figure out why only the first line within the if statement is the only line being executed.

bool messageDisplayed = false;

private void timer1_Tick(object sender, EventArgs e)
{
    var position = axVLCPlugin21.input.Position;

    label1.Text = string.Format("{0}% completed", position * 100);

    if (position > 0.8 && messageDisplayed == false)
    {
        MessageBox.Show("80 has been passed");
        messageDisplayed = true;
    }
}

The messagebox kept showing up constantly, leading me to believe messageDisplayed = true was never being set, so I set a break point to check.

And sure enough, it enters the if statement, shows the messagebox, and then restarts the event- without ever going to messageDisplayed = true;

Is anyone able to see if I'm going wrong somewhere?


Solution

  • MessageBox.Show() "pauses" the execution of the code. messageDisplayed = true; is triggered when you close the MessageBox.

    Consequently, modify your code as follows:

    if (position > 0.8 && !messageDisplayed)
    {
        messageDisplayed = true;
        MessageBox.Show("80 has been passed");
    }