App.xaml.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using GeekDesk.Constant;
  2. using GeekDesk.Util;
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Input;
  6. using System.Windows.Threading;
  7. namespace GeekDesk
  8. {
  9. /// <summary>
  10. /// App.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class App : Application
  13. {
  14. System.Threading.Mutex mutex;
  15. public App()
  16. {
  17. this.Startup += new StartupEventHandler(App_Startup);
  18. Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
  19. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  20. }
  21. private void App_Startup(object sender, StartupEventArgs e)
  22. {
  23. mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out bool ret);
  24. if (!ret)
  25. {
  26. System.Threading.Thread.Sleep(2000);
  27. mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret);
  28. if (!ret)
  29. {
  30. MessageUtil.SendMsgByWName(
  31. "GeekDesk_Main_" + Constants.MY_UUID,
  32. "ShowApp"
  33. );
  34. Environment.Exit(0);
  35. }
  36. }
  37. }
  38. void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  39. {
  40. e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  41. Mouse.OverrideCursor = null;
  42. LogUtil.WriteErrorLog(e, "未捕获异常!");
  43. if (Constants.DEV)
  44. {
  45. MessageBox.Show("GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!");
  46. }
  47. }
  48. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  49. {
  50. LogUtil.WriteErrorLog(e, "严重异常!");
  51. MessageBox.Show("GeekDesk遇到未知问题崩溃!");
  52. }
  53. public static void DoEvents()
  54. {
  55. var frame = new DispatcherFrame();
  56. Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
  57. new DispatcherOperationCallback(
  58. delegate (object f)
  59. {
  60. ((DispatcherFrame)f).Continue = false;
  61. return null;
  62. }), frame);
  63. Dispatcher.PushFrame(frame);
  64. }
  65. }
  66. }