1
0

App.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using GeekDesk.Constant;
  2. using GeekDesk.MyThread;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using Microsoft.Win32;
  6. using System;
  7. using System.Diagnostics;
  8. using System.Windows;
  9. using System.Windows.Input;
  10. using System.Windows.Interop;
  11. using System.Windows.Media;
  12. using System.Windows.Threading;
  13. namespace GeekDesk
  14. {
  15. /// <summary>
  16. /// App.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. System.Threading.Mutex mutex;
  21. public App()
  22. {
  23. this.Startup += new StartupEventHandler(App_Startup);
  24. Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
  25. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  26. SystemEvents.PowerModeChanged += OnPowerModeChanged;
  27. }
  28. private void App_Startup(object sender, StartupEventArgs e)
  29. {
  30. //RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; //禁用硬件加速
  31. mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out bool ret);
  32. if (!ret)
  33. {
  34. System.Threading.Thread.Sleep(2000);
  35. mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret);
  36. if (!ret)
  37. {
  38. MessageUtil.SendMsgByWName(
  39. "GeekDesk_Main_" + Constants.MY_UUID,
  40. "ShowApp"
  41. );
  42. Environment.Exit(0);
  43. }
  44. }
  45. }
  46. //电源监听
  47. private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
  48. {
  49. switch (e.Mode)
  50. {
  51. case PowerModes.Resume:
  52. // 系统从休眠状态唤醒
  53. LogUtil.WriteLog("System resumed from sleep.");
  54. ProcessUtil.ReStartApp();
  55. break;
  56. case PowerModes.Suspend:
  57. // 系统进入休眠状态
  58. LogUtil.WriteLog("System is going to sleep.");
  59. break;
  60. }
  61. }
  62. void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  63. {
  64. e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  65. Mouse.OverrideCursor = null;
  66. LogUtil.WriteErrorLog(e, "未捕获异常!");
  67. if (Constants.DEV)
  68. {
  69. MessageBox.Show("GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!");
  70. }
  71. }
  72. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  73. {
  74. LogUtil.WriteErrorLog(e, "严重异常!");
  75. //MessageBox.Show("GeekDesk遇到未知问题崩溃!");
  76. }
  77. public static void DoEvents()
  78. {
  79. var frame = new DispatcherFrame();
  80. Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
  81. new DispatcherOperationCallback(
  82. delegate (object f)
  83. {
  84. ((DispatcherFrame)f).Continue = false;
  85. return null;
  86. }), frame);
  87. Dispatcher.PushFrame(frame);
  88. }
  89. }
  90. }