using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ScreenShowManage { static class Program { //static Boolean createdNew; ////同步基元变量 //static System.Threading.Mutex instance = new System.Threading.Mutex(true, "TestSingleStart", out createdNew); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { //if (!createdNew) //{ // MessageBox.Show("系统已经启动当前程序"); // Application.Exit(); // return; //} //设置应用程序处理异常方式:ThreadException处理 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainView()); } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string str = GetExceptionMsg(e.Exception, e.ToString()); LoggingHelper.Fatal("严重错误ThreadException:" + str); //ReStartManager(); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); LoggingHelper.Fatal("严重错误UnhandledException:" + str); ReStartManager(); } /// <summary> /// 生成自定义异常消息 /// </summary> /// <param name="ex">异常对象</param> /// <param name="backStr">备用异常消息:当ex为null时有效</param> /// <returns>异常字符串文本</returns> static string GetExceptionMsg(Exception ex, string backStr) { StringBuilder builder = new StringBuilder(); builder.AppendLine("****************************异常文本****************************"); builder.AppendLine("【出现时间】:" + DateTime.Now.ToString()); if (ex != null) { builder.AppendLine("【异常类型】:" + ex.GetType().Name); builder.AppendLine("【异常信息】:" + ex.Message); builder.AppendLine("【堆栈调用】:" + ex.StackTrace); } else { builder.AppendLine("【未处理异常】:" + backStr); } builder.AppendLine("***************************************************************"); return builder.ToString(); } /// <summary> /// 开启重启程序 /// </summary> static void ReStartManager() { string processPath = AppDomain.CurrentDomain.BaseDirectory; Process winHello = new Process(); try { winHello.StartInfo.UseShellExecute = true; winHello.StartInfo.FileName = processPath + "ReStartManager.exe"; winHello.StartInfo.CreateNoWindow = true; winHello.Start(); Application.Exit(); } catch (Exception ex) { LoggingHelper.Fatal("ReStartManager开启重启程序失败:" + ex.StackTrace); } } } }
文章评论