Terminate application after unhandled exception

前端 未结 2 1420
长情又很酷
长情又很酷 2020-12-18 11:15

I have a problem in a WPF application. I wrote this code:

public partial class App : Application
{
    public App()
    {
        AppDomain.CurrentDoma         


        
相关标签:
2条回答
  • 2020-12-18 11:33

    You can also use Application.Exit() or System.Environment.Exit(exitCode) to immediately shut down your application after you have shown your error dialog box.

    0 讨论(0)
  • 2020-12-18 11:48

    You can avoid multiple messageboxes by initializing a static boolean firstTime to true and use the code within the Exception handler:

    void MyHandler(object sender, UnhandledExceptionEventArgs e) 
    { 
       if (firstTime){
            Exception exception = e.ExceptionObject as Exception; 
            MessageBox.Show(exception.Message, "ERROR", 
                            MessageBoxButton.OK, MessageBoxImage.Error); 
            firstTime = false;
       }else{
            // Now kill the process....
       }
    } 
    

    To terminate the process do this, within the MyHandler:

    System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
    proc.Kill();
    
    0 讨论(0)
提交回复
热议问题