123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Windows;
- using System.IO;
- using System.Drawing;
- using AGV_WPF_Global;
- using System.Threading;
- using System.Diagnostics;
- namespace AGV_WPF
- {
- /// <summary>
- /// App.xaml 的交互逻辑
- /// </summary>
- public partial class App : Application
- {
- public App()
- {
- //if (!CheckApp())
- //{
- // MessageBox.Show("请在任务管理器中结束先前进程");
- // this.Shutdown();
- //}
- Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- DirectoryInfo info = new DirectoryInfo("Exception");
- if (!info.Exists)
- {
- Directory.CreateDirectory("Exception");
- }
- DirectoryInfo info1 = new DirectoryInfo("Link");
- if (!info1.Exists)
- {
- Directory.CreateDirectory("Link");
- }
- GlobalPara.LoadData();
- }
-
- void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- try
- {
- DateTime time = DateTime.Now;
- //MessageBox.Show("线程异常:"+e.ExceptionObject);
- FileStream stream = File.Open(@"Exception\程序异常.txt", FileMode.Append, FileAccess.Write);
- StreamWriter writer = new StreamWriter(stream);
- Exception exc = e.ExceptionObject as Exception;
- if (exc != null)
- {
- 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);
- writer.WriteLine(msg);
- writer.Close();
- stream.Close();
- }
- if (GlobalPara.IsExceptionSavePic)
- {
- try
- {
- 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);
- Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
- Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- Graphics g = Graphics.FromImage(bmp);
- g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
- bmp.Save(@"Exception\" + fileName + ".png");
- }
- catch (System.Exception ex)
- {
-
- throw ex;
- }
- }
- }
- catch (Exception ex)
- {
- //放到捕获事件的处理代码后,重启程序,需要时加上重启的参数
- //CmdStartCTIProc(System.Windows.Forms.Application.ExecutablePath, "cmd params");
- throw ex;
- }
- }
- void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
- {
- DateTime time = DateTime.Now;
- try
- {
- FileStream stream = File.Open(@"Exception\程序异常.txt", FileMode.Append, FileAccess.Write);
- StreamWriter writer = new StreamWriter(stream);
- Exception exc = e.Exception;
- if (exc != null)
- {
- 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);
- writer.WriteLine(msg);
- writer.Close();
- stream.Close();
- }
- e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
- if (GlobalPara.IsExceptionSavePic)
- {
- try
- {
- 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);
- Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
- Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- Graphics g = Graphics.FromImage(bmp);
- g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
- bmp.Save(@"Exception\" + fileName + ".png");
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- catch(Exception ex)
- {
- //放到捕获事件的处理代码后,重启程序,需要时加上重启的参数
- //CmdStartCTIProc(System.Windows.Forms.Application.ExecutablePath, "cmd params");
- MessageBox.Show(ex.Message);
- }
- }
-
- static Mutex appMutex = new Mutex(true, "CasunAgv");
- bool CheckApp()
- {
- bool result = false;
- if (appMutex.WaitOne(600))
- result = true;
- else
- result = false;
- return result;
- }
- /// <summary>
- /// 在命令行窗口中执行
- /// </summary>
- /// <param name="sExePath"></param>
- /// <param name="sArguments"></param>
- static void CmdStartCTIProc(string sExePath, string sArguments)
- {
- Process p = new Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = false;
- p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- p.Start();
- p.StandardInput.WriteLine(sExePath + " " + sArguments);
- p.StandardInput.WriteLine("exit");
- p.Close();
- System.Threading.Thread.Sleep(2000);//必须等待,否则重启的程序还未启动完成;根据情况调整等待时间
- }
- /// <summary>
- /// 重启系统
- /// </summary>
- void ResetSystem()
- {
- //重启程序,需要时加上重启的参数
- System.Diagnostics.ProcessStartInfo cp = new System.Diagnostics.ProcessStartInfo();
- cp.FileName = System.Windows.Forms.Application.ExecutablePath;
- cp.Arguments = "cmd params";
- cp.UseShellExecute = true;
- System.Diagnostics.Process.Start(cp);
- }
- }
- }
|