RtApplication.cs 6.9 KB

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