Terminate application after unhandled exception

前端 未结 2 1425
长情又很酷
长情又很酷 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: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();
    

提交回复
热议问题