2

I'm trying to show a message box when pressing a button on a winforms application, but the MessageBox hangs and never returns a value.

private void btnApply_Click(object sender, EventArgs e)
    {
        bool current = false;
        if (cmbEmergencyLandingMode.SelectedIndex > 0)
        {
            if (m_WantedData != false)
            {
                DialogResult dr = MessageBox.Show("Are you sure you want to enable Emergency Landing mode?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                //i never get to this point
                current = (dr == DialogResult.Yes);
            }
        }

        if (m_WantedData == current)
        {
            //do something
        }
        else if (m_WantedData != null)
        { 
            //do something else
        }
    }

Edit: Ok so i got it to work by handling the button event on a background worker:

private void btnApply_Click(object sender, EventArgs e)
    {
        if (!bwApply.IsBusy) 
            bwApply.RunWorkerAsync(cmbEmergencyLandingMode.SelectedIndex);
    }

    void bwApply_DoWork(object sender, DoWorkEventArgs e)
    {
        bool current = false;
        int selectedIndex = (int)e.Argument;

        if (selectedIndex > 0)
        {
            if (m_WantedData != false)
            {
                DialogResult dr = MessageBox.Show(
                    "Are you sure you want to enable Emergency Landing mode?",
                    "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                current = (dr == DialogResult.Yes);
            }
        }

        if (m_WantedData == current)
        {
            //do something
        }
        else if (m_WantedData != null)
        {
            //do something else
        }
    }

thanks to everyone who helped!

Nataly87
  • 233
  • 2
  • 10
  • 4
    the code you have should work... – lc. Jul 16 '14 at 06:16
  • Are you sure? This should work.. http://stackoverflow.com/questions/5414270/messagebox-buttons and http://stackoverflow.com/questions/16334323/event-handlers-on-message-box-buttons – Soner Gönül Jul 16 '14 at 06:17
  • well it was working, and after i made some changes that have nothing to do with this piece of code, it started hanging and the whole application gets stuck. – Nataly87 Jul 16 '14 at 06:20
  • Have you rebuild your app? (F6) Also you may have to delete the old .exe´s in your *..\Projects\Debug* folder – BudBrot Jul 16 '14 at 06:33
  • "*after i made some changes that have nothing to do with this piece of code, it started hanging*", it sounds like thats your real problem then – Sayse Jul 16 '14 at 06:38
  • @BudBrot did not help. still hangs – Nataly87 Jul 16 '14 at 06:45
  • @Sayse you know what, even if it was relevant, why does the messagebox hang? – Nataly87 Jul 16 '14 at 06:46
  • I have no idea what the other code is so I have no idea what the other code is doing, but since messagebox is modal it could mean that something somewhere is having to wait. – Sayse Jul 16 '14 at 06:48
  • @Sayse this application is a flight simulator, and i added a window to go over the video that shows the flight data. the form that opens the messagebox has nothing to do with both of those other forms – Nataly87 Jul 16 '14 at 06:54
  • But MessageBox is an application modal dialog... " A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application. "[Reference](http://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx) Is something somewhere else waiting for input? – Sayse Jul 16 '14 at 07:04
  • @Sayse no. everything on the screen is just showing information. – Nataly87 Jul 16 '14 at 07:13
  • Its impossible for me to help any more... it could be that your trying to reference the focused window which of course would be the messagebox when you expect it to be something else. without more detail I can't help. – Sayse Jul 16 '14 at 07:14
  • I really would like to reproduce this problem in my application. Something there that I don't know. +1 for that – Shell Jul 16 '14 at 07:19
  • Did you set a breakpoint on the line `current = (dr == DialogResult.Yes);` to see whether that line is executed? From the code I guess that the message box does not "hang", but if the actions performed after the message box take some time, your window doesn't repaint itself, so the message box appears to be still active while it is not. – Thorsten Dittmar Jul 16 '14 at 07:21
  • @ThorstenDittmar the reason i decided it hangs is because i set a breakepoint on that line and it never gets there. what actually happens is the messagebox appears, and after i press "Yes" or "No" the entire application acts as if the messagebox is still open but there is no messagebox, and i never get to the breakpoint – Nataly87 Jul 16 '14 at 07:26
  • Could it be your UI thread is blocked by waiting for something? You say you made changes that have nothing to do with the above piece of code. Well, obviously they *do* have something to do with that piece of code. Otherwise it would work. Post what you changed and the way it was before. Otherwise we won't be able to help. – Thorsten Dittmar Jul 16 '14 at 07:46
  • @ThorstenDittmar i just added another form that is not related to this one. – Nataly87 Jul 16 '14 at 07:49
  • Shot in the dark: how many UI threads do you have running? Also, what sort of multithreading code do you have, if any? It could be that the MessageBox is on the wrong thread / scheduler. – Sameer Singh Jul 16 '14 at 08:23
  • @SameerSingh actually there are a lot of threads (it came that way, and theres pretty much nothing i can do about it right now). i actually tried invoking the messagebox on the calling form, and on the main form, and it still acts the same. – Nataly87 Jul 16 '14 at 08:33
  • Pointless asking about this code when the problem is, as you admit, elsewhere. Use your revision control system to go back to a version that works, and identify the change that introduced the problem. – David Heffernan Jul 16 '14 at 08:42
  • Let's continue this [in chat](http://chat.stackoverflow.com/rooms/57404/nataly87s-messagebox-hangs-problem) – Sameer Singh Jul 16 '14 at 12:18

2 Answers2

1

Have you tried specifying the message box owner, as below?

DialogResult dr = MessageBox.Show(
    this,
    "Are you sure you want to enable Emergency Landing mode?",
    "Warning!",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Warning);

I've seen an application behave like this if a message box is shown while there are concurrent changes to the UI behind the message box. If the above doesn't help, try reducing the stress on UI processing (i.e. WinAPI messaging) by handling the event asynchronously, as below.

public delegate void ApplyDelegate();

private void btnApply_Click(object sender, EventArgs e)
{
   btnApply.BeginInvoke(new ApplyDelegate(ApplyAsync));
}

private void ApplyAsync()
{
    bool current = false;
    if (cmbEmergencyLandingMode.SelectedIndex > 0)
    {
        if (m_WantedData != false)
        {
            DialogResult dr = MessageBox.Show(
                this,
                "Are you sure you want to enable Emergency Landing mode?",
                "Warning!",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            //i never get to this point
            current = (dr == DialogResult.Yes);
        }
    }

    if (m_WantedData == current)
    {
        //do something
    }
    else if (m_WantedData != null)
    { 
        //do something else
    }
}
groverboy
  • 1,133
  • 8
  • 20
0

Try to show message without assigning parent form in MessageBox.Show() method. I don't know why but, i had similar problem when i have loaded modal window.

if (m_WantedData != false)
{
    if (MessageBox.Show("Sample Message", "Are you sure you want to enable Emergency Landing mode?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    {
        //DO YOUR STUFF HERE IF YES
    }
    else
    {

    }
}
Shell
  • 6,818
  • 11
  • 39
  • 70
  • i tried that too. still hangs. i even tried restarting the computer because it just started hanging without any reason – Nataly87 Jul 16 '14 at 06:28
  • is there any break point in any control validate event. It may hang your computer when cursor leave the focus. In that case press `Ctrl + Alt + Del`. that will navigate the visual studio screen and you can continue debugging. – Shell Jul 16 '14 at 06:36
  • What happens when you press `Ctrl + Alt + Del` when application hanged. if you are using vista, win 7 or win 8 then select task manager from the list. It may break the current process. – Shell Jul 16 '14 at 07:24
  • well it says the application is running, because it is. its the messagebox that is stuck. – Nataly87 Jul 16 '14 at 07:31