Skip to content

Problem displaying a messageBox

Alex

Asked Aug 2, 2007, 7:32 AM EDT4 answers
Share
LinkedInX
Hi,

I'm working on a WinForm application with .NET CF 2.0 and I have a background running thread that needs to notify the user about various events. The problem is that everytime I want to notify the user with a MessageBox about an event, the application exits once the messagebox is displayed...I get the following message in the output window: The thread 0x2eda2a2 has exited with code 0 (0x0).

However, if I display the message in a label of my form, everything works well...here is the code I use:
  1. public void DisplayMessage(string message)
  2.         {
  3.             if (InvokeRequired)
  4.             {
  5.                 DisplayMessageDelegate displayMessageDelegate = new DisplayMessageDelegate(DisplayMessage);
  6.                 this.Invoke(displayMessageDelegate, new object[] { message });
  7.             }
  8.             else
  9.             {
  10.                 this.labelStatus.Text = message;  // Works well
  11.                 MessageBox.Show(message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); // Application exits when called
  12.                
  13.             }
  14.         }

Thanks in advance!
alex

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    try putting the method that you are delegating to in a separate method.  Looks like you are invoking back to the same method you are in, which I suspect is dangerous.

    0
  • 0

    My delegate used to call a different method, but I've seen in the "Safe, Simple Multithreading in Windows Form" tutorial [http://msdn2.microsoft.com/en-us/library/ms951089.aspx] that their delegate was invoking back the same method, which avoids creating 2 methods doing almost  the same thing.

    Thanks!

    0
  • Mike Gold
    0
    I would try as an experiment to create a message box from a Windows Form and calling ShowDialog instead of using MessageBox.  See if you still get the same problem of exiting. You can also try my scrollable message box and see if it causes an exit.
    0
  • Mike Gold
    0

    I just tried it with your code and it seems to work fine.  Here is the code I called in my Form to ensure it was being called from a separate thread.  Could some other part of your program be causing an exit?  I would turn on exception handling under Exceptions in the Debug Menu and check the CLR Exceptions :

    C#
    private void Form1_Load(object sender, EventArgs e)
     {
     // TODO: This line of code loads data into the 'fPNWINDDataSet.Customers' table. You can move, or remove it, as needed.
     Thread oMessage = new Thread(new ThreadStart(ShowMessage));
     oMessage.Start();
    }
    private void ShowMessage()
     {
        DisplayMessage("testing");
     }
    private delegate void DisplayMessageDelegate(string msg);
    public void DisplayMessage(string message)
    {
    if (InvokeRequired)
    {
    DisplayMessageDelegate displayMessageDelegate = new DisplayMessageDelegate(DisplayMessage);
    this.Invoke(displayMessageDelegate, new object[] { message });
    }
    else
    {
    // this.labelStatus.Text = message; // Works well

    MessageBox.Show(message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); // Application exits when called

    }

    }

    0