using AIDiagnosisDemo.Infrastucture; using System; using System.Diagnostics; using System.Windows; using System.Windows.Threading; namespace AIDiagnosisDemo { /// /// Interaction logic for App.xaml /// public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { CheckedOpenRepeatedly(); AppDomain.CurrentDomain.UnhandledException += OnDomainOnUnhandledException; Current.DispatcherUnhandledException += OnDispatcherUnhandledException; base.OnStartup(e); } private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { Logger.Error($"OnDispatcherUnhandledException:{e.Exception}"); e.Handled = true; Dispatcher.Invoke(() => { MessageBox.Show($"OnDispatcherUnhandledException{e.Exception}"); }); } private void OnDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e) { Logger.Error($"OnDomainOnUnhandledException:{e.ExceptionObject}"); Dispatcher.Invoke(() => { MessageBox.Show($"OnDomainOnUnhandledException{e.ExceptionObject}"); }); } /// /// 判断是否重复打开 /// private void CheckedOpenRepeatedly() { var thisProcess = Process.GetCurrentProcess(); foreach (var item in Process.GetProcessesByName(thisProcess.ProcessName)) { if (thisProcess.Id != item.Id) { MessageBox.Show("警告:程序正在运行中! 请不要重复打开程序!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); thisProcess.Kill(); } } } } }