C# 程序异常终止

 

   public App()
        {
       
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is System.Exception)
            {
                Exception ex = (System.Exception)e.ExceptionObject;
                MessageBox.Show(ex.Message);
            }
            Console.WriteLine("异常退出:" + e.ToString());
        }

        //private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        //{
        //    Console.WriteLine("99999:  " + msg);
        //    switch (msg)
        //    {
        //        case 0x0024:/* WM_GETMINMAXINFO */

        //            handled = true;
        //            break;
        //        default: break;
        //    }
        //    return (System.IntPtr)0;
        //}

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("我们很抱歉,当前应用程序遇到一些问题,该操作已经终止,请重新启动.", "意外的操作", MessageBoxButton.OK, MessageBoxImage.Information);
            SystemProxy proxy = new SystemProxy();
            proxy.UpdateL2TP(true);
            proxy.Stop();
            Application.Current.Shutdown();
            //string proName = Process.GetCurrentProcess().ProcessName;
            // Process[] localByName = Process.GetProcessesByName(proName);//因为可以同时启动多
            e.Handled = true;

        }

 

你可能感兴趣的:(C#)