Desolator
Desolator

Reputation: 22759

MessageBox not showing when "Unhandled Exception" triggers in winforms

so I was trying to add "AppDomain.CurrentDomain.UnhandledException" handler to my application and it worked OK if I log the error to a text file. but when I try to use a MessageBox it never pops. is it another bug in .Net? any ideas?

here is my code sample:

   AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Application.Run(new Form1());

and here is my handler method:

static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
    try
    {
       Exception ex = (Exception)e.ExceptionObject;

       MessageBox.Show("Whoops! Please contact the developers with "
                   + "the following information:\r\n\r\n" + ex.Message + ex.StackTrace,
                   "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

    }
    finally
    {
       Application.Exit();
    }
}

EDIT: I've tried all of the possible options but I still can't see the MessageBox. Now the problem is when I run it from Visual C# (debug mode) it perfectly shows the box. but when I run the application directly from the debug/release folders it doesn't shows the MessageBox and the Application keeps running like there is no error is happening...

Upvotes: 3

Views: 1884

Answers (3)

Mal Clarke
Mal Clarke

Reputation: 307

A bit late to the party but no workable solution is shown here. I added

Application.DoEvents();

Just before the message box and the MessageBox now shows.

Hope this helps somebody out.

Upvotes: 0

punker76
punker76

Reputation: 14611

this example works for me in debug mode and release mode with vs2010 or not:

using System;
using System.Windows.Forms;

namespace WinFormsStackOverflowSpielWiese
{
  internal static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    private static void Main() {
      System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
      System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
      AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }

    private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
      try {
        var exception = e.Exception != null ? e.Exception.Message + e.Exception.StackTrace : string.Empty;
        MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
                        "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
      }
      finally {
        Application.Exit();
      }
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
      try {
        var exception = e.ExceptionObject is Exception ? ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).StackTrace : string.Empty;
        MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
                        "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
      }
      finally {
        Application.Exit();
      }
    }
  }
}

code for the form

EDIT now with a timer, with the same result....

using System.Windows.Forms;

namespace WinFormsStackOverflowSpielWiese
{
  public partial class Form1 : Form
  {
    private System.Threading.Timer myTimer;

    public Form1() {
      this.InitializeComponent();

      this.myTimer = new System.Threading.Timer(state =>
                                                  {
                                                    var i = 0;
                                                    var s = 100 / i;
                                                  }
                                                , null, 5000, 5000);
    }
  }
}

Upvotes: 1

S2S2
S2S2

Reputation: 8502

The case might be because Unhandled Exceptions cause the application to terminate silently and UnhandledExceptionEventHandler handles non-UI thread exceptions.

See Application.SetUnhandledExceptionMode Method, AppDomain.UnhandledException Event and Application.ThreadException Event

EDIT: Try setting Application.SetUnhandledExceptionMode as per first link:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //add this line

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);    
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

Upvotes: 1

Related Questions