RtApplication.cs 6.0 KB

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