简体   繁体   中英

Close application without .net framework's error prompt window

Codes handles unhandled exceptions as below in my project.

   static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledExceptionEventArgs)
        {
            Exception _Exception = (Exception)_UnhandledExceptionEventArgs.ExceptionObject;
            OnUnwantedCloseSendEmail(_Exception.Message);
        }

I am using OnUnwantedCloseSendEmail method for sending email for error reports. Last line of OnUnwantedCloseSendEmail method is Application.Restart();

When this approach correctly works, .net framework show an prompt window for error as below and application not close and restart until press the quit button.

在此处输入图片说明

How can i exit application without this prompt and how can i apply this approach when application frozen too.

You may want to look into the Application.SetUnhandledExceptionMode method. Calling this with the UnhandledExceptionMode.ThrowException parameter will prevent Winform to route exceptions down to the Application.ThreadException event and thus this dialog won't ever show up.

You can also alter the app.config file to get the same result:

<configuration>
  <system.windows.forms jitDebugging="true"/>
</configuration>

Personnaly I prefer the hard-coded route.

Just so we're clear: this will only get rid of the dialog , not solve the actual assembly loading or app freezing problem. :)

You should be able to catch everything with this

    [STAThread]
    public static void Main()
    {
        // let IDE to handle exceptions
        if (System.Diagnostics.Debugger.IsAttached)
            Run();
        else
            try
            {
                Application.ThreadException += Application_ThreadException;
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Run();
            }
            catch (Exception e)
            {
                // catch exceptions outside of Application.Run
                UnhandledException(e);
            }
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        // catch non-ui exceptions
        UnhandledException(e.ExceptionObject as Exception);
    }

    private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        // catch ui exceptions
        UnhandledException(e.Exception);
    }

    private static void UnhandledException(Exception e)
    {
        try
        {
            // here we restart app
        }
        catch
        {
            // if we are here - things are really really bad
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM