zoukankan      html  css  js  c++  java
  • 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 : "")));
            }
    
        }
  • 相关阅读:
    【Elasticsearch 技术分享】—— ES 常用名词及结构
    【Elasticsearch 技术分享】—— Elasticsearch ?倒排索引?这都是什么?
    除了读写锁,JUC 下面还有个 StampedLock!还不过来了解一下么?
    小伙伴想写个 IDEA 插件么?这些 API 了解一下!
    部署Microsoft.ReportViewe
    关于TFS强制undo他人check out
    几段查看数据库表占用硬盘空间的tsql
    How to perform validation on sumbit only
    TFS 2012 Disable Multiple Check-out
    在Chrome Console中加载jQuery
  • 原文地址:https://www.cnblogs.com/yonsy/p/5606882.html
Copyright © 2011-2022 走看看