C# 程序異常管理方案

C# 程序異常管理方案

一、程序出現未處理異常(程序中未捕獲異常、添加異常處理)html

二、程序添加全局異常捕獲 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);//執行異常處理
        }

三、執行異常處理服務器

    <方案1>自動執行:適用於服務器程序

    <方案2>彈出異常處理窗口:提供給用戶處理,適用於客戶端程序

可供選擇的異常解決方案:
//一、獲取異常信息多線程

<1>捕獲catch異常 
<2>彈窗請求用戶描述操做內容:適用於客戶端程序

//二、反饋開發人員this

    //一、郵件方式
    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);
    }
    //二、短信通知
    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();
    }

//三、記錄異常日誌url

    //存入數據庫異常日誌 Tip:最好使用不一樣數據庫。避免數據庫異常問題
    public void SaveLog()
    {
        //數據庫操做
    }

//四、自動重啓程序spa

<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);
    }

//五、直接關閉.net

    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),在程序退出時觸發全部線程的終止條件。 }

 

出處:https://blog.csdn.net/u010265681/article/details/76651754線程

相關文章
相關標籤/搜索