应用程序有Bug崩溃重启的案例

发表于:2018-3-21 11:09

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:救赎者    来源:博客园

  1、程序主界面代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsAppThreadRestart
{
static class Program
{
public static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().GetType());
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Log.Error(e.Exception);
//throw new Exception("线程未知异常", e.Exception);
if (MessageBox.Show($"异常信息:{e.Exception.Message},是否重启应用程序或者联系开发工程师?", "线程异常", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
CmdStartProc1(Application.ExecutablePath, "<nul");
//StartProc2();
Application.Exit();
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
Log.Error(ex);
if (MessageBox.Show($"异常信息:{ex.Message},是否重启应用程序或者联系开发工程师?", "应用程序异常", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
CmdStartProc1(Application.ExecutablePath, "<nul");
Application.Exit();
}
}
/// <summary>
/// 在命令行窗口中执行
/// </summary>
/// <param name="sExePath"></param>
/// <param name="sArguments"></param>
static void CmdStartProc1(string sExePath, string sArguments)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;//true表示不显示黑框,false表示显示dos界面
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
p.StandardInput.WriteLine(sExePath);
p.StandardInput.WriteLine("exit");
p.Close();
System.Threading.Thread.Sleep(100);//必须等待,否则重启的程序还未启动完成;根据情况调整等待时间
}
static void StartProc2()
{
//重启程序,需要时加上重启的参数
ProcessStartInfo cp = new ProcessStartInfo();
cp.FileName = Application.ExecutablePath;
//cp.Arguments = "cmd params";
cp.UseShellExecute = true;
Process.Start(cp);
}
}
}
  2、窗体界面的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsAppThreadRestart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = 0;
int b = 9 / a;
}
}
}

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号