WinForm程序全局捕捉異常處理辦法


如何全局捕捉Winform程序異常呢,當然是從程序啟動入口的Program類下的Main()方法定義了,下面看下這個類怎么寫的吧

    static class Program
    {
        static string RunFormFullName
        {
            get
            {
                string setRunFormFullName = CIPACE.Sys.Configuration.RunFormFullName;
                if (setRunFormFullName == null)
                    setRunFormFullName = DigiForm.SETRUNFORMFULLNAME;

                return setRunFormFullName;
            }
        }
        /// <summary>
        ///   應用程序的主入口點。
        /// </summary>
        public static ApplicationContext context;


        [STAThread]
        private static void Main()
        {
            try
            {
                //處理未捕獲的異常   
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //處理UI線程異常   
                Application.ThreadException += Application_ThreadException;
                //處理非UI線程異常   
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

                var aProcessName = Process.GetCurrentProcess().ProcessName;
                if ((Process.GetProcessesByName(aProcessName)).GetUpperBound(0) > 0)
                {
                    MessageBox.Show(@"系統已經在運行中,如果要重新啟動,請從進程中關閉...", @"系統警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    context = new ApplicationContext();
                    Application.Idle += Application_Idle; //注冊程序運行空閑去執行主程序窗體相應初始化代碼
                    Application.Run(context);
                }
            }
            catch (Exception ex)
            {
                LogNet.Log.WriteLog("Main", ex);
                MessageBox.Show("系統出現異常:" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : ""))+",請重啟程序。");
                
                DigiForm digiForm = new DigiForm();
                digiForm.UpdateAppSettings(DigiForm.RUNFORMFULLNAME, DigiForm.LOGINFORMFULLNAME);
            }
        }

        private static void Application_Idle(object sender, EventArgs e)
        {
            Application.Idle -= Application_Idle;
            if (context.MainForm == null)
            {
                Form form = new LoginForm();
                context.MainForm = form;
                form.Show();
            }
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var ex = e.Exception;
            if (ex != null)
            {
                LogNet.Log.WriteLog("Application_ThreadException", ex);
            }

            MessageBox.Show("系統出現異常:" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : "")));
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
            if (ex != null)
            {
                LogNet.Log.WriteLog("CurrentDomain_UnhandledException", ex);
            }

            MessageBox.Show("系統出現異常:" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : "")));
        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM