zoukankan      html  css  js  c++  java
  • C# 程序异常管理方案

    C# 程序异常管理方案

    1、程序出现未处理异常(程序中未捕获异常、添加异常处理)

    2、程序添加全局异常捕获 tip:程序已处理异常不在捕获范围内。

            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                //全局异常捕捉
                Application.ThreadException += Application_ThreadException; //UI线程异常
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; //多线程异常
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmMain());
            }
    
            //UI线程异常
            static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                WinformException.FrmBugReport.ShowBug(e.Exception);//执行异常处理
            }
    
            //多线程异常
            static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                WinformException.FrmBugReport.ShowBug((Exception)e.ExceptionObject);//执行异常处理
            }

    3、执行异常处理

        <方案1>自动执行:适用于服务器程序
    
        <方案2>弹出异常处理窗口:提供给用户处理,适用于客户端程序

    可供选择的异常解决方案:
    //1、获取异常信息

    <1>捕获catch异常 
    <2>弹窗请求用户描述操作内容:适用于客户端程序

    //2、反馈开发人员

        //1、邮件方式
        public static string SendEmail()
        {
            //执行发邮件
            string Sender = "sender@****.com.cn";//发件人
            string Password = "*******";//发件人密码(部分邮箱可以设为空)
            string MailServer = "*****.****.****.com.cn";//邮箱服务器域名
            int smtpPort = 25; //邮件端口
            string Recevicer= "recevicer@****.com.cn";//收件人 可以为多人)
            System.Net.Mail.SmtpClient sc = new SmtpClient(MailServer, smtpPort);
            NetworkCredential nc = new NetworkCredential(UserName, Password);
            sc.Credentials = nc;
            MailMessage MyEmailMessage = new MailMessage();
            MyEmailMessage.From = new MailAddress(UserName);
            MyEmailMessage.To.Add(new MailAddress(temp)); 
            MyEmailMessage.Subject = "";//主题
            MyEmailMessage.Body = content; //内容
            MyEmailMessage.IsBodyHtml = true; //是否为Html、纯文本
            MyEmailMessage.Priority = MailPriority.High; //优先级
            sc.Send(MyEmailMessage);
        }
        //2、短信通知
        public static string SendSms()
        {
            //执行发短信
            string targeturl = url.Trim().ToString();
            HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
            hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
            hr.Method = "GET";
            hr.Timeout = 30 * 60 * 1000;
            WebResponse hs = hr.GetResponse();
            Stream sr = hs.GetResponseStream();
            StreamReader ser = new StreamReader(sr, Encoding.Default);
            strRet = ser.ReadToEnd();
        }

    //3、记录异常日志

        //存入数据库异常日志 Tip:最好使用不同数据库。避免数据库异常问题
        public void SaveLog()
        {
            //数据库操作
        }

    //4、自动重启程序

    <way1>
        public void AutoReStart()
        {
            //执行重启
            Process p = new Process();
            p.StartInfo.FileName = "你的启动项.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);//必须等待,否则重启的程序还未启动完成;根据情况调整等待时间
        }
        <way2>
        public void AutoReStart()
        {
            //重启程序,需要时加上重启的参数
            System.Diagnostics.ProcessStartInfo cp = new System.Diagnostics.ProcessStartInfo();
            cp.FileName = Application.ExecutablePath;
            cp.Arguments = "重启参数";
            cp.UseShellExecute = true;
            System.Diagnostics.Process.Start(cp);
        }

    //5、直接关闭

        public void CloseApp()//选择一种, 异常崩溃时建议 Environment.Exit(0);
        {
            this.Close();//只是关闭当前窗体。
            Application.ExitThread();//退出当前线程上的消息循环,并关闭该线程上的所有窗口。 也会失灵
            Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵        
            Environment.Exit(0); //前面三种方法都不能很好的退出程序,此方法可以完全退出程序,这个要强制得多。
            Process.GetCurrentProcess().Kill();//此方法完全奏效,绝对是完全退出。 
            主线程设置为后台进程。方法是将主线程的 isBackground = true。据说,这样在关闭主程序时后关闭主线程,并关闭所有的线程。
            .netFrame Work compact下是没有强制退出程序进程,替代方式:
            System.Diagnostics.Process tt = 
            System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id); tt.Kill();(暴力) 良好的程序设计应该是所有的线程都有条件可以结束循环以退出(包括timer),在程序退出时触发所有线程的终止条件。 }
  • 相关阅读:
    安全测试的概述和用例设计
    性能测试(四)常见调优
    性能测试(三)常见的性能测试缺陷
    Jmeter(七)六种参数化的方式
    Jmeter(六)所有的断言
    接口测试的问题解答
    ES学习
    flutter 之BottomNavigationBar属性
    flutter StaggeredGridView.countBuilder 上方取消空白
    flutter升级、回退到指定版本---mac版
  • 原文地址:https://www.cnblogs.com/tranw/p/6189639.html
Copyright © 2011-2022 走看看