App.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using SCADA.CommonCtrl.WpfControl;
  2. using SCADA.CommonLib;
  3. using SCADA.CommonLib.Service;
  4. using SCADA_DAQ.Plugin.Core.AutoUpdater;
  5. using SCADA_DAQ.Plugin.CoreUI;
  6. using System;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Media.Imaging;
  15. using System.Xml.Linq;
  16. namespace SCADA_DAQ
  17. {
  18. /// <summary>
  19. /// App.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class App : Application, IApp
  22. {
  23. private static SystemControl systemControl;
  24. private string _sessionid;
  25. /// <summary>
  26. /// 会话ID
  27. /// </summary>
  28. public string SessionId => _sessionid ?? (_sessionid = Guid.NewGuid().ToString());
  29. internal Task updateCheckTask;
  30. /// <summary>
  31. /// 产品ID
  32. /// </summary>
  33. public string ProductId { get; } = System.Windows.Forms.Application.ProductName;
  34. /// <summary>
  35. /// 版本ID
  36. /// </summary>
  37. public string UidVersion { get; set; }
  38. /// <summary>
  39. /// 启动时间
  40. /// </summary>
  41. public DateTime StartTime { get; }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public IServiceHost ServiceHost { get; set; }
  46. private App()
  47. {
  48. StartTime = DateTime.Now;
  49. Console.WriteLine($"App Start {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
  50. updateCheckTask = Task.Factory.StartNew(() =>
  51. {
  52. try
  53. {
  54. var _checkUpdate = true;
  55. if (File.Exists("UserConfig.xml"))
  56. {
  57. var xmlFile = XDocument.Load("UserConfig.xml");
  58. var node = xmlFile.Descendants("App.AutoUpdate").FirstOrDefault();
  59. var val = node?.Value;
  60. bool.TryParse(val, out _checkUpdate);
  61. }
  62. if (_checkUpdate)
  63. {
  64. var task = UpdateHelper.CheckUpdate();
  65. task.Wait();
  66. }
  67. }
  68. catch (Exception)
  69. {
  70. }
  71. });
  72. try
  73. {
  74. var appServer = Env.WebServerAddress.Value;
  75. Trace.WriteLine($"Server:{appServer}");
  76. var task = Task.Factory.StartNew(Env.ParameterInit);
  77. var win = new MainWindow();
  78. MainWindow = win;
  79. win.FrmClosingWait += Win_FrmClosingWait;
  80. systemControl = SystemControl.Create(this);
  81. var iconPath = "Content/Img/favicon.ico";
  82. if (File.Exists(iconPath))
  83. {
  84. MainWindow.Icon = new BitmapImage(new Uri(iconPath, UriKind.RelativeOrAbsolute));
  85. }
  86. MainWindow.Show();
  87. task.Wait();
  88. systemControl.SystemInit();
  89. }
  90. catch (Exception ex)
  91. {
  92. updateCheckTask?.Wait(10 * 1000);
  93. Trace.TraceError(ex.ToString());
  94. }
  95. }
  96. private void Win_FrmClosingWait(object sender, EventArgs e)
  97. {
  98. SystemControl.GetInstance().ApplicationDispose(0);
  99. }
  100. private void Application_Startup(object sender, StartupEventArgs e)
  101. {
  102. }
  103. private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
  104. {
  105. systemControl.ApplicationSessionEnding(this, e);
  106. }
  107. private void Application_Exit(object sender, ExitEventArgs e)
  108. {
  109. systemControl.ApplicationExit(e.ApplicationExitCode);
  110. }
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. /// <returns></returns>
  115. public bool RestartHost()
  116. {
  117. return systemControl.RestartHost();
  118. }
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. /// <param name="runAs"></param>
  123. public void Restart(bool runAs = false)
  124. {
  125. MainWindow.Close();
  126. Exit += (s, e) =>
  127. {
  128. new Thread(() =>
  129. {
  130. Thread.Sleep(1000);
  131. string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
  132. Process myNewProcess = new Process();
  133. myNewProcess.StartInfo.FileName = strAppFileName;
  134. myNewProcess.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
  135. if (runAs == true)
  136. {
  137. myNewProcess.StartInfo.Verb = "runas";
  138. }
  139. myNewProcess.Start();
  140. }).Start();
  141. };
  142. }
  143. /// <summary>
  144. ///
  145. /// </summary>
  146. public void Restart()
  147. {
  148. Dispatcher.Invoke(new Action(() =>
  149. {
  150. Restart(false);
  151. }));
  152. }
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. public void ReLoadLicense()
  157. {
  158. systemControl.LoadLicense();
  159. }
  160. /// <summary>
  161. /// 显示toast
  162. /// </summary>
  163. /// <param name="message">toast内容</param>
  164. /// <param name="toastType"></param>
  165. /// <param name="showTime">toast 时间 短:2500 长:3500</param>
  166. public void ShowToast(object message, ControlStyle toastType = ControlStyle.Default, int showTime = 2000)
  167. {
  168. Dispatcher.BeginInvoke(new Action(() =>
  169. {
  170. var toastItem = new ToastItem() { Message = message, ControlStyle = toastType };
  171. var showPannel = SCADA.CommonCtrl.WpfHelper.VisualHelper.GetChildObject<Panel>(MainWindow, "PART_ToastLayer");
  172. toastItem.Show(showPannel, showTime);
  173. }));
  174. }
  175. /// <summary>
  176. /// 显示短toast
  177. /// </summary>
  178. /// <param name="message">toast内容</param>
  179. /// <param name="toastType"></param>
  180. public void ShowShortToast(object message, ControlStyle toastType)
  181. {
  182. ShowToast(message, toastType, 2000);
  183. }
  184. /// <summary>
  185. /// 显示长toast
  186. /// </summary>
  187. /// <param name="message">toast内容</param>
  188. /// <param name="toastType"></param>
  189. public void ShowLongToast(object message, ControlStyle toastType)
  190. {
  191. ShowToast(message, toastType, 3500);
  192. }
  193. }
  194. }