App.xaml.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using GeekDesk.Constant;
  2. using GeekDesk.Util;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.IO;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Media.Animation;
  9. using System.Windows.Threading;
  10. namespace GeekDesk
  11. {
  12. /// <summary>
  13. /// App.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class App : Application
  16. {
  17. System.Threading.Mutex mutex;
  18. public App()
  19. {
  20. this.Startup += new StartupEventHandler(App_Startup);
  21. Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
  22. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  23. }
  24. private void App_Startup(object sender, StartupEventArgs e)
  25. {
  26. mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out bool ret);
  27. if (!ret)
  28. {
  29. System.Threading.Thread.Sleep(2000);
  30. mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret);
  31. if (!ret)
  32. {
  33. Environment.Exit(0);
  34. }
  35. }
  36. }
  37. void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  38. {
  39. e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  40. LogUtil.WriteErrorLog(e, "未捕获异常!");
  41. if (Constants.DEV)
  42. {
  43. MessageBox.Show("GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!");
  44. }
  45. }
  46. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  47. {
  48. LogUtil.WriteErrorLog(e, "严重异常!");
  49. MessageBox.Show("GeekDesk遇到未知问题崩溃!");
  50. }
  51. }
  52. }