Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent "Send error report to Microsoft"

I'm working on a rather large project, and its unlikely will catch everything. I've found the event that notifies me of unhandled exceptions, however I haven't found a way to programmatically shut off the windows error dialog. Ideally, if there is an unhandled exception, I would like to have that event fired, provide a dialog box telling the user that there is a problem, and then to close gracefully. Is there any way to do this? I realize I could wrap the highest layer in a try catch, but I was hoping for something a little more elegant.

like image 454
Steve Avatar asked Jan 23 '09 15:01

Steve


1 Answers

This is what we did.

static void Main() {
    try
    {
        SubMain();
    }
    catch (Exception e)
    {
        HandleUnhandledException(e);
    }
}

private static void SubMain()
{
    // Setup unhandled exception handlers
    AppDomain.CurrentDomain.UnhandledException += // CLR
       new UnhandledExceptionEventHandler(OnUnhandledException);
     Application.ThreadException += // Windows Forms
       new System.Threading.ThreadExceptionEventHandler(
           OnGuiUnhandledException);
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new frmMain());
}

// CLR unhandled exception
private static void OnUnhandledException(Object sender,
   UnhandledExceptionEventArgs e)
{
    HandleUnhandledException(e.ExceptionObject);
}

// Windows Forms unhandled exception
private static void OnGuiUnhandledException(Object sender,
   System.Threading.ThreadExceptionEventArgs e)
{
    HandleUnhandledException(e.Exception);
}
like image 52
Aaron Smith Avatar answered Sep 23 '22 22:09

Aaron Smith