App.xaml.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. Environment.Exit(0);
  31. }
  32. }
  33. }
  34. void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  35. {
  36. e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  37. Mouse.OverrideCursor = null;
  38. LogUtil.WriteErrorLog(e, "未捕获异常!");
  39. if (Constants.DEV)
  40. {
  41. MessageBox.Show("GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!");
  42. }
  43. }
  44. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  45. {
  46. LogUtil.WriteErrorLog(e, "严重异常!");
  47. MessageBox.Show("GeekDesk遇到未知问题崩溃!");
  48. }
  49. public static void DoEvents()
  50. {
  51. var frame = new DispatcherFrame();
  52. Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
  53. new DispatcherOperationCallback(
  54. delegate (object f)
  55. {
  56. ((DispatcherFrame)f).Continue = false;
  57. return null;
  58. }), frame);
  59. Dispatcher.PushFrame(frame);
  60. }
  61. }
  62. }