App.xaml.cs 7.0 KB

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