App.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.IO;
  8. using System.Drawing;
  9. using AGV_WPF_Global;
  10. using System.Threading;
  11. using System.Diagnostics;
  12. using System.Threading.Tasks;
  13. namespace AGV_WPF
  14. {
  15. /// <summary>
  16. /// App.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. public App()
  21. {
  22. //if (!CheckApp())
  23. //{
  24. // MessageBox.Show("请在任务管理器中结束先前进程");
  25. // this.Shutdown();
  26. //}
  27. Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
  28. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  29. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  30. DirectoryInfo info = new DirectoryInfo("Exception");
  31. if (!info.Exists)
  32. {
  33. Directory.CreateDirectory("Exception");
  34. }
  35. DirectoryInfo info1 = new DirectoryInfo("Link");
  36. if (!info1.Exists)
  37. {
  38. Directory.CreateDirectory("Link");
  39. }
  40. GlobalPara.LoadData();
  41. }
  42. void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  43. {
  44. DateTime time = DateTime.Now;
  45. try
  46. {
  47. FileStream stream = File.Open(@"Exception\程序异常.txt", FileMode.Append, FileAccess.Write);
  48. StreamWriter writer = new StreamWriter(stream);
  49. Exception exc = e.Exception;
  50. if (exc != null)
  51. {
  52. string msg = string.Format("[{0}]UI线程:\r\nMessage:{1}\r\nStackTrace:\r\n{2}", time.ToString("yyyy-MM-dd HH:mm:ss"), exc.Message, exc.StackTrace);
  53. writer.WriteLine(msg);
  54. writer.Close();
  55. stream.Close();
  56. }
  57. e.SetObserved();//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  58. }
  59. catch (Exception ex)
  60. {
  61. //放到捕获事件的处理代码后,重启程序,需要时加上重启的参数
  62. //CmdStartCTIProc(System.Windows.Forms.Application.ExecutablePath, "cmd params");
  63. throw ex;
  64. }
  65. }
  66. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  67. {
  68. try
  69. {
  70. DateTime time = DateTime.Now;
  71. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  72. FileStream stream = File.Open(@"Exception\程序异常.txt", FileMode.Append, FileAccess.Write);
  73. StreamWriter writer = new StreamWriter(stream);
  74. Exception exc = e.ExceptionObject as Exception;
  75. if (exc != null)
  76. {
  77. string msg = string.Format("[{0}]线程:\r\nMessage:{1}\r\nStackTrace:\r\n{2}", time.ToString("yyyy-MM-dd HH:mm:ss"), exc.Message, exc.StackTrace);
  78. writer.WriteLine(msg);
  79. writer.Close();
  80. stream.Close();
  81. }
  82. if (GlobalPara.IsExceptionSavePic)
  83. {
  84. try
  85. {
  86. string fileName = string.Format("{0}{1:d2}{2:d2}{3:d2}{4:d2}{5:d2}", time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second);
  87. Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
  88. Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  89. Graphics g = Graphics.FromImage(bmp);
  90. g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
  91. bmp.Save(@"Exception\" + fileName + ".png");
  92. }
  93. catch (System.Exception ex)
  94. {
  95. throw ex;
  96. }
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. //放到捕获事件的处理代码后,重启程序,需要时加上重启的参数
  102. //CmdStartCTIProc(System.Windows.Forms.Application.ExecutablePath, "cmd params");
  103. throw ex;
  104. }
  105. }
  106. void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  107. {
  108. DateTime time = DateTime.Now;
  109. try
  110. {
  111. FileStream stream = File.Open(@"Exception\程序异常.txt", FileMode.Append, FileAccess.Write);
  112. StreamWriter writer = new StreamWriter(stream);
  113. Exception exc = e.Exception;
  114. if (exc != null)
  115. {
  116. string msg = string.Format("[{0}]UI线程:\r\nMessage:{1}\r\nStackTrace:\r\n{2}", time.ToString("yyyy-MM-dd HH:mm:ss"), exc.Message, exc.StackTrace);
  117. writer.WriteLine(msg);
  118. writer.Close();
  119. stream.Close();
  120. }
  121. e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  122. if (GlobalPara.IsExceptionSavePic)
  123. {
  124. try
  125. {
  126. string fileName = string.Format("{0}{1:d2}{2:d2}{3:d2}{4:d2}{5:d2}", time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second);
  127. Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
  128. Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  129. Graphics g = Graphics.FromImage(bmp);
  130. g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
  131. bmp.Save(@"Exception\" + fileName + ".png");
  132. }
  133. catch (System.Exception ex)
  134. {
  135. MessageBox.Show(ex.Message);
  136. }
  137. }
  138. }
  139. catch(Exception ex)
  140. {
  141. //放到捕获事件的处理代码后,重启程序,需要时加上重启的参数
  142. //CmdStartCTIProc(System.Windows.Forms.Application.ExecutablePath, "cmd params");
  143. MessageBox.Show(ex.Message);
  144. }
  145. }
  146. static Mutex appMutex = new Mutex(true, "CasunAgv");
  147. bool CheckApp()
  148. {
  149. bool result = false;
  150. if (appMutex.WaitOne(600))
  151. result = true;
  152. else
  153. result = false;
  154. return result;
  155. }
  156. /// <summary>
  157. /// 在命令行窗口中执行
  158. /// </summary>
  159. /// <param name="sExePath"></param>
  160. /// <param name="sArguments"></param>
  161. static void CmdStartCTIProc(string sExePath, string sArguments)
  162. {
  163. Process p = new Process();
  164. p.StartInfo.FileName = "cmd.exe";
  165. p.StartInfo.UseShellExecute = false;
  166. p.StartInfo.RedirectStandardInput = true;
  167. p.StartInfo.RedirectStandardOutput = true;
  168. p.StartInfo.RedirectStandardError = true;
  169. p.StartInfo.CreateNoWindow = false;
  170. p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  171. p.Start();
  172. p.StandardInput.WriteLine(sExePath + " " + sArguments);
  173. p.StandardInput.WriteLine("exit");
  174. p.Close();
  175. System.Threading.Thread.Sleep(2000);//必须等待,否则重启的程序还未启动完成;根据情况调整等待时间
  176. }
  177. /// <summary>
  178. /// 重启系统
  179. /// </summary>
  180. void ResetSystem()
  181. {
  182. //重启程序,需要时加上重启的参数
  183. System.Diagnostics.ProcessStartInfo cp = new System.Diagnostics.ProcessStartInfo();
  184. cp.FileName = System.Windows.Forms.Application.ExecutablePath;
  185. cp.Arguments = "cmd params";
  186. cp.UseShellExecute = true;
  187. System.Diagnostics.Process.Start(cp);
  188. }
  189. }
  190. }