App.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using IvfTl.Control.Services;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Threading;
  13. namespace TLTest
  14. {
  15. /// <summary>
  16. /// Interaction logic for App.xaml
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. private static Mutex instance;
  21. /// <summary>
  22. /// 分支说明:15分钟定时换气,下位机不能接管换气
  23. /// </summary>
  24. public App()
  25. {
  26. Log4netHelper.WriteLog("程序启动");
  27. //首先注册开始和退出事件
  28. this.Startup += new StartupEventHandler(App_Startup);
  29. //this.Exit += new ExitEventHandler(App_Exit);
  30. }
  31. protected override void OnStartup(StartupEventArgs e)
  32. {
  33. bool isNotRunning; //互斥体判断
  34. instance = new Mutex(true, "ivf_tl_ControlMain", out isNotRunning); //同步基元变量
  35. if (!isNotRunning) // 如果不是未运行状态
  36. {
  37. MessageBox.Show("程序已启动 ");
  38. App.Current.Shutdown();
  39. return;
  40. }
  41. base.OnStartup(e);
  42. }
  43. protected override void OnExit(ExitEventArgs e)
  44. {
  45. base.OnExit(e);
  46. Log4netHelper.WriteLog("程序退出");
  47. }
  48. private void App_Exit(object sender, ExitEventArgs e)
  49. {
  50. Log4netHelper.WriteLog("App_Exit");
  51. }
  52. private void App_Startup(object sender, StartupEventArgs e)
  53. {
  54. //UI线程未捕获异常处理事件
  55. this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
  56. //Task线程内未捕获异常处理事件
  57. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  58. //非UI线程未捕获异常处理事件
  59. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  60. ChangeLanguage(ConfigurationManager.AppSettings["Language"].ToString());
  61. }
  62. /// <summary>
  63. /// 非UI线程未捕获异常处理事件
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. /// <exception cref="NotImplementedException"></exception>
  68. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  69. {
  70. try
  71. {
  72. if (e.IsTerminating)
  73. {
  74. Log4netHelper.WriteLog($"非UI线程发生致命错误,程序即将终止");
  75. }
  76. if (e.ExceptionObject is Exception ex)
  77. {
  78. if (ex.InnerException != null)
  79. {
  80. Log4netHelper.WriteLog($"非UI线程异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
  81. }
  82. Log4netHelper.WriteLog($"非UI线程异常:{ex.Message}{ex.StackTrace}");
  83. }
  84. else
  85. {
  86. Log4netHelper.WriteLog($"非UI线程异常:异常对象类型不是Exception");
  87. }
  88. }
  89. catch (Exception exx)
  90. {
  91. if (exx.InnerException != null)
  92. {
  93. Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常详细:{exx.InnerException.Message}{exx.InnerException.StackTrace}");
  94. }
  95. Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常:{exx.Message}{exx.StackTrace}");
  96. }
  97. }
  98. /// <summary>
  99. /// Task线程内未捕获异常处理事件
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. /// <exception cref="NotImplementedException"></exception>
  104. private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
  105. {
  106. try
  107. {
  108. if (e.Exception.InnerException != null)
  109. {
  110. Log4netHelper.WriteLog($"Task线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
  111. }
  112. Log4netHelper.WriteLog($"Task线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
  113. e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
  114. }
  115. catch (Exception ex)
  116. {
  117. if (ex.InnerException != null)
  118. {
  119. Log4netHelper.WriteLog($"捕获Task线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
  120. }
  121. Log4netHelper.WriteLog($"捕获Task线程异常时发生异常:{ex.Message}{ex.StackTrace}");
  122. }
  123. finally
  124. {
  125. e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
  126. }
  127. }
  128. /// <summary>
  129. /// UI线程未捕获异常处理事件
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. /// <exception cref="NotImplementedException"></exception>
  134. private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  135. {
  136. try
  137. {
  138. if (e.Exception.InnerException != null)
  139. {
  140. Log4netHelper.WriteLog($"UI线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
  141. }
  142. Log4netHelper.WriteLog($"UI线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
  143. //把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出
  144. }
  145. catch (Exception ex)
  146. {
  147. //此时程序出现严重异常,将强制结束退出
  148. if (ex.InnerException != null)
  149. {
  150. Log4netHelper.WriteLog($"捕获UI线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
  151. }
  152. Log4netHelper.WriteLog($"捕获UI线程异常时发生异常:{ex.Message}{ex.StackTrace}");
  153. }
  154. finally
  155. {
  156. e.Handled = true;
  157. }
  158. }
  159. public void ChangeLanguage(string languageName)
  160. {
  161. try
  162. {
  163. ResourceDictionary langRd = null;
  164. #if DEBUG
  165. string xamlFilePath = @"C:\PersonalSpace\work\1 VisualWorkSpace\ControlLan\" + languageName;
  166. #else
  167. string xamlFilePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Language\\{languageName}";
  168. #endif
  169. if (!File.Exists(xamlFilePath))
  170. {
  171. Log4netHelper.WriteLog($"切换语言失败,配置文件不存在:{xamlFilePath}");
  172. return;
  173. }
  174. using (var stream = new FileStream(xamlFilePath, FileMode.Open))
  175. {
  176. langRd = System.Windows.Markup.XamlReader.Load(stream) as ResourceDictionary;
  177. }
  178. if (langRd != null)
  179. {
  180. int count = Application.Current.Resources.MergedDictionaries.Count;
  181. if (count >= 3)
  182. {
  183. Application.Current.Resources.MergedDictionaries.RemoveAt(count - 1);
  184. }
  185. Application.Current.Resources.MergedDictionaries.Add(langRd);
  186. }
  187. else
  188. {
  189. Log4netHelper.WriteLog($"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}");
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. Log4netHelper.WriteLog($"切换语言异常,{JsonConvert.SerializeObject(ex)}");
  195. return;
  196. }
  197. }
  198. }
  199. }