C# 程序异常管理方案
2024-08-28 22:20:19
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、获取异常信息
<>捕获catch异常
<>弹窗请求用户描述操作内容:适用于客户端程序
//2、反馈开发人员
//1、邮件方式
public static string SendEmail()
{
//执行发邮件
string Sender = "sender@****.com.cn";//发件人
string Password = "*******";//发件人密码(部分邮箱可以设为空)
string MailServer = "*****.****.****.com.cn";//邮箱服务器域名
int smtpPort = ; //邮件端口
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 = * * ;
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();//必须等待,否则重启的程序还未启动完成;根据情况调整等待时间
}
<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();
{
this.Close();//只是关闭当前窗体。
Application.ExitThread();//退出当前线程上的消息循环,并关闭该线程上的所有窗口。 也会失灵
Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵
Environment.Exit(); //前面三种方法都不能很好的退出程序,此方法可以完全退出程序,这个要强制得多。
Process.GetCurrentProcess().Kill();//此方法完全奏效,绝对是完全退出。
主线程设置为后台进程。方法是将主线程的 isBackground = true。据说,这样在关闭主程序时后关闭主线程,并关闭所有的线程。
.netFrame Work compact下是没有强制退出程序进程,替代方式:
System.Diagnostics.Process tt =
System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
tt.Kill();(暴力)
良好的程序设计应该是所有的线程都有条件可以结束循环以退出(包括timer),在程序退出时触发所有线程的终止条件。
}
最新文章
- 学习Maven之Maven Clean Plugin
- [SSIS] 在脚本里面使用数据库连接字符串进行查询等处理, 入坑
- windows7 64位系统pl/sql 客户端的安装
- CentOS下配置Hadoop集群:java.net.NoRouteToHostException: No route to host问题的解决
- 杭电1097-A hard puzzle
- 你今天Python了吗?(上)
- [Bower] Bower
- AFNetworking 使用总结
- IOS7修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
- Ewebeditor最新漏洞和漏洞指数
- Linux 下安装 Memcached 和 PHP 开启 Memcached 扩展
- Shell排序
- BZOJ 4569: [Scoi2016]萌萌哒 [并查集 倍增]
- CSV空行问题
- C++游戏开发需要阅读的书籍
- np.linespace使用方法
- SOS does not support the current target architecture解决方法
- Powershell About Active Directory Server
- ABAP git客户端
- React 相关开发工具