views:

411

answers:

4

Is there a way (other than WerAddExcludedApplication which won't work in Windows XP) to disable the window "Application has encountered a problem and needs to close" from appearing when my application crashes?

alt text

(image taken from Bil Simser' blog)

I need this to work in Windows XP.

A: 

Don't have unhandled exceptions.

David B
you can't handle all exceptions... at least it's not always possible
Krzysztof Koźmic
Yes, you can. You handle all exceptions you can think of, and then add a handler for unhandled exceptions to catch the ones you didn't. That works about 99.9% of the time; for the other 0.01%, fix your bad code.
Ken White
Not a very helpful answer!
Philip Wallace
+1  A: 

You need to handle the Application.ThreadException and AppDomain.CurrentDomain.UnhandledException events.

Simon
These are events so you can act upon them, not really do much of handling, but that's besides the point. You may have no other option that to close the app once it gets corrupted, and what to do then?
Krzysztof Koźmic
Plus it won't work anyway - even if you subscribe to the event, the window will show up anyway
Krzysztof Koźmic
Oh, you should definitely terminate the application if you get to this stage, but I don't see a big problem with presenting a custom crash dialog. As for your second comment, all I can say is that it works here.
Simon
@Krzysztof If you call AppDomain.FailFast() (I might not be remembering the class 100% right) in your event handler, it won't. But again, use the WER reports to your advantage!
Paul Betts
+1  A: 

My suggestion is, instead of disabling this dialog, register with Microsoft so that you can see the error reports we capture! This way, you can put this dialog to use, instead of trying to suppress it.

http://msdn.microsoft.com/en-us/isv/bb190483.aspx

Paul Betts
thanks, but in this case it *really* is not a solution. I need specifically what I ask about - ability to restart the app when it crashes without that dialog popping up.
Krzysztof Koźmic
+3  A: 

This will work, allowing you to show your own custom dialog:

Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);

Here is a full sample from MSDN:

Thread newThread = null;

// Starts the application. 
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Add the event handler for handling non-UI thread exceptions to the event. 
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    // Runs the application.
    Application.Run(new ErrorHandlerForm());
}

// Programs the button to throw an exception when clicked.
private void button1_Click(object sender, System.EventArgs e)
{
    throw new ArgumentException("The parameter was invalid");
}

// Start a new thread, separate from Windows Forms, that will throw an exception.
private void button2_Click(object sender, System.EventArgs e)
{
    ThreadStart newThreadStart = new ThreadStart(newThread_Execute);
    newThread = new Thread(newThreadStart);
    newThread.Start();
}

// The thread we start up to demonstrate non-UI exception handling. 
void newThread_Execute()
{
    throw new Exception("The method or operation is not implemented.");
}

// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
private static void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t)
{
    DialogResult result = DialogResult.Cancel;
    try
    {
        result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception);
    }
    catch
    {
        try
        {
            MessageBox.Show("Fatal Windows Forms Error",
                "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
        }
        finally
        {
            Application.Exit();
        }
    }

    // Exits the program when the user clicks Abort.
    if (result == DialogResult.Abort)
        Application.Exit();
}

// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
// NOTE: This exception cannot be kept from terminating the application - it can only 
// log the event, and inform the user about it. 
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    try
    {
        Exception ex = (Exception)e.ExceptionObject;
        string errorMsg = "An application error occurred. Please contact the adminstrator " +
            "with the following information:\n\n";

        // Since we can't prevent the app from terminating, log this to the event log.
        if (!EventLog.SourceExists("ThreadException"))
        {
            EventLog.CreateEventSource("ThreadException", "Application");
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "ThreadException";
        myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
    }
    catch (Exception exc)
    {
        try
        {
            MessageBox.Show("Fatal Non-UI Error",
                "Fatal Non-UI Error. Could not write the error to the event log. Reason: "
                + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        finally
        {
            Application.Exit();
        }
    }
}

// Creates the error message and displays it.
private static DialogResult ShowThreadExceptionDialog(string title, Exception e)
{
    string errorMsg = "An application error occurred. Please contact the adminstrator " +
        "with the following information:\n\n";
    errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
    return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,
        MessageBoxIcon.Stop);
}
Philip Wallace
And what if I'm not in a WinForms application? but say... Console app, where I don't use Application class?
Krzysztof Koźmic