RtApplication.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Reflection;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Forms;
  7. using Aitex.Core.Backend;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.NotifyTrayIcons;
  11. using MECF.Framework.RT.Core.Backend;
  12. namespace MECF.Framework.RT.Core.Applications
  13. {
  14. public class RtApplication: Singleton<RtApplication>
  15. {
  16. static ThreadExceptionEventHandler ThreadHandler = new ThreadExceptionEventHandler(Application_ThreadException);
  17. private ShowWindowNotifyIcon _trayIcon;
  18. private IRtInstance _instance;
  19. private static bool _ignoreError;
  20. public static BackendMainView MainView { get; set; }
  21. private PassWordView _loginWindow = new PassWordView();
  22. private static Mutex _mutex;
  23. protected System.Windows.Application application { get; set; }
  24. public void Initialize(IRtInstance instance)
  25. {
  26. AdminRelauncher.RelaunchIfNotAdmin();
  27. application = System.Windows.Application.Current;
  28. application.DispatcherUnhandledException += OnUnhandledException;
  29. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  30. AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
  31. //Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.
  32. System.Windows.Forms.Application.ThreadException += ThreadHandler;
  33. System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  34. _instance = instance;
  35. CheckAnotherInstanceRunning(instance.SystemName);
  36. MainView = new BackendMainView();
  37. MainView.Icon = _instance.TrayIcon;
  38. MainView.Title = _instance.SystemName + " Console";
  39. _loginWindow.Title = _instance.SystemName + " RT Backend Login";
  40. _loginWindow.Icon = _instance.TrayIcon;
  41. _ignoreError = _instance.KeepRunningAfterUnknownException;
  42. if (_instance.EnableNotifyIcon)
  43. {
  44. _trayIcon = new ShowWindowNotifyIcon(_instance.SystemName, _instance.TrayIcon);
  45. _trayIcon.ExitWindow += TrayIconExitWindow;
  46. _trayIcon.ShowMainWindow += TrayIconShowMainWindow;
  47. _trayIcon.ShowBallon(_instance.SystemName, "Start running");
  48. }
  49. InitSystem();
  50. if (_instance.DefaultShowBackendWindow)
  51. {
  52. MainView.Show();
  53. }
  54. }
  55. protected void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  56. {
  57. if (e.Exception != null)
  58. {
  59. LOG.Write(e.Exception);
  60. if (e.Exception.InnerException != null)
  61. LOG.Write(e.Exception.InnerException);
  62. }
  63. e.Handled = true;
  64. }
  65. void CheckAnotherInstanceRunning(string appName)
  66. {
  67. _mutex = new Mutex(true, appName, out bool createNew);
  68. if (!createNew)
  69. {
  70. if (!_mutex.WaitOne(1000))
  71. {
  72. string msg = $"{appName} is already running,can not start again";
  73. System.Windows.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.MessageBoxButton.OK, MessageBoxImage.Warning, System.Windows.MessageBoxResult.No,
  74. System.Windows.MessageBoxOptions.DefaultDesktopOnly);
  75. Environment.Exit(0);
  76. }
  77. }
  78. }
  79. static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  80. {
  81. System.Windows.Forms.Application.ThreadException -= ThreadHandler;
  82. }
  83. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  84. {
  85. ShowMessageDialog(e.Exception);
  86. }
  87. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  88. {
  89. ShowMessageDialog((Exception)e.ExceptionObject);
  90. }
  91. static void ShowMessageDialog(Exception ex)
  92. {
  93. LOG.Write(ex);
  94. if (!_ignoreError)
  95. {
  96. string message = string.Format(" RT unknown exception:{0},\n", DateTime.Now);
  97. System.Windows.MessageBox.Show(message + ex.Message, "Unexpected exception", System.Windows.MessageBoxButton.YesNo,
  98. System.Windows.MessageBoxImage.Exclamation,
  99. System.Windows.MessageBoxResult.No,
  100. System.Windows.MessageBoxOptions.DefaultDesktopOnly);
  101. //Environment.Exit(0);
  102. }
  103. }
  104. private void TrayIconExitWindow()
  105. {
  106. if (System.Windows.MessageBox.Show("Are you sure you want to exit system?", _instance.SystemName, System.Windows.MessageBoxButton.YesNo,
  107. System.Windows.MessageBoxImage.Exclamation,
  108. System.Windows.MessageBoxResult.No,
  109. System.Windows.MessageBoxOptions.DefaultDesktopOnly) == System.Windows.MessageBoxResult.Yes)
  110. {
  111. if (_trayIcon != null)
  112. {
  113. _trayIcon.ShowBallon(_instance.SystemName, "Stop running");
  114. }
  115. Terminate();
  116. }
  117. }
  118. private void TrayIconShowMainWindow(bool show)
  119. {
  120. if (MainView == null)
  121. return;
  122. if (show )
  123. {
  124. if (!MainView.IsVisible)
  125. {
  126. if (!_loginWindow.IsVisible)
  127. {
  128. _loginWindow.Reset();
  129. _loginWindow.ShowDialog();
  130. if (_loginWindow.VerificationResult)
  131. MainView.Show();
  132. }
  133. }
  134. else
  135. {
  136. MainView.Show();
  137. }
  138. }
  139. else
  140. {
  141. MainView.Hide();
  142. }
  143. }
  144. private void InitSystem()
  145. {
  146. if (_instance.Loader != null)
  147. {
  148. _instance.Loader.Initialize();
  149. }
  150. }
  151. public void Terminate()
  152. {
  153. if (_instance.Loader != null)
  154. {
  155. _instance.Loader.Terminate();
  156. }
  157. if (_trayIcon != null)
  158. {
  159. _trayIcon.Dispose();
  160. }
  161. if (MainView != null)
  162. {
  163. MainView.Close();
  164. }
  165. System.Windows.Application.Current.Shutdown(0);
  166. Environment.Exit(0);
  167. }
  168. }
  169. }