ThreadException

在windows窗体程序中,使用 ThreadException 事件来处理 UI 线程异常,使用 UnhandledException 事件来处理非 UI 线程异常。ThreadException可以阻止应用程序终止。具体使用方法如下:

           [STAThread]
           static void Main()
           {
               Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
               Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
               AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
               
               //获取该应用程序的名称,判断是否在进程中存在,如果存在,则需要关闭再重新启动
               var processName=Process.GetCurrentProcess().ProcessName;
               if((Process.GetProcessesByName(aProcessName)).GetUpperBound(0) > 0) 
               {
                   MessageBox.Show("程序已经启动,需要关闭才能重新开启!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               }
               else
               {
                   Application.EnableVisualStyles();
                   Application.SetCompatibleTextRenderingDefault(false);
                   Application.Run(new Form());
               }   
           }
           private static void UIThreadException(object sender, ThreadExceptionEventArgs e)
           {
               try
               {
                   string errorMsg = "Windows窗体线程异常";
                   LogUnhandledException(errorMsg,e.Exception);
                   //MessageBox.Show(errorMsg + e.Exception.Message + Environment.NewLine + e.Exception.StackTrace);
               }
               catch
               {
                   MessageBox.Show("不可恢复的Windows窗体异常,应用程序将退出!");
               }
           }
           private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
           {
               try
               {
                   Exception ex = (Exception)e.ExceptionObject;
                   string errorMsg = "非Windows窗体线程异常";
                   LogUnhandledException(errorMsg,ex);
                   //MessageBox.Show(errorMsg + ex.Message + Environment.NewLine + ex.StackTrace);
               }
               catch
               {
                   MessageBox.Show("不可恢复的非Windows窗体线程异常,应用程序将退出!");
               }
           }

          //异常日志处理
          private static void LogUnhandledException(string message, Exception ex)
          {
              string path = AppDomain.CurrentDomain.BaseDirectory+"Logs";
              if(path!=null&&path!="")
              {
                  if (!(Directory.Exists(path)))
                  {
                     Directory.CreateDirectory(path); //创建日志文件夹
                  }
                  path += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));

                  using (var sw = new StreamWriter(path, true, Encoding.Default))
                  {
                     sw.WriteLine("**************************************************************************************");
                     sw.WriteLine(DateTime.Now.ToString());
                     if (ex != null)
                     {
                        sw.WriteLine("异常信息:");
                        sw.WriteLine(message);
                        sw.WriteLine("【Message】" + ex.Message);
                        sw.WriteLine("【ErrorType】:" + ex.GetType());
                        sw.WriteLine("【TargetSite】" + ex.TargetSite);
                        sw.WriteLine("【StackTrace】" + ex.StackTrace);
                     }
                     else sw.WriteLine("Exception Is Null");
                     sw.WriteLine();
                  }
              }
          }
          
          
  
世界上最可怕事情是比我们优秀的人比我们还努力
原文地址:https://www.cnblogs.com/AlexOneBlogs/p/7398442.html