WPF异常处理

  • Post author:
  • Post category:其他


///

/// App.xaml 的交互逻辑

///

 public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            //常量,显示,默认
            // SerchMD5();
            Process currentProcess = Process.GetCurrentProcess();

            foreach (Process item in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
            {

                if (item.Id != currentProcess.Id &&
                (item.StartTime - currentProcess.StartTime).TotalMilliseconds <= 0)
                {
                    item.Kill();
                    item.WaitForExit();
                }
            }

            base.OnStartup(e);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception error = (Exception)e.ExceptionObject;
            MessageBox.Show("异常:" + error.Message);
            Environment.Exit(0);
        }
         
    }



版权声明:本文为qq_43393792原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。