App.xaml.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using AIDiagnosisDemo.Infrastucture;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Windows;
  5. using System.Windows.Threading;
  6. namespace AIDiagnosisDemo
  7. {
  8. /// <summary>
  9. /// Interaction logic for App.xaml
  10. /// </summary>
  11. public partial class App : Application
  12. {
  13. protected override void OnStartup(StartupEventArgs e)
  14. {
  15. CheckedOpenRepeatedly();
  16. AppDomain.CurrentDomain.UnhandledException += OnDomainOnUnhandledException;
  17. Current.DispatcherUnhandledException += OnDispatcherUnhandledException;
  18. base.OnStartup(e);
  19. }
  20. private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  21. {
  22. Logger.Error($"OnDispatcherUnhandledException:{e.Exception}");
  23. e.Handled = true;
  24. Dispatcher.Invoke(() =>
  25. {
  26. MessageBox.Show($"OnDispatcherUnhandledException{e.Exception}");
  27. });
  28. }
  29. private void OnDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
  30. {
  31. Logger.Error($"OnDomainOnUnhandledException:{e.ExceptionObject}");
  32. Dispatcher.Invoke(() =>
  33. {
  34. MessageBox.Show($"OnDomainOnUnhandledException{e.ExceptionObject}");
  35. });
  36. }
  37. /// <summary>
  38. /// 判断是否重复打开
  39. /// </summary>
  40. private void CheckedOpenRepeatedly()
  41. {
  42. var thisProcess = Process.GetCurrentProcess();
  43. foreach (var item in Process.GetProcessesByName(thisProcess.ProcessName))
  44. {
  45. if (thisProcess.Id != item.Id)
  46. {
  47. MessageBox.Show("警告:程序正在运行中! 请不要重复打开程序!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  48. thisProcess.Kill();
  49. }
  50. }
  51. }
  52. }
  53. }