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

应用程序有bug崩溃重启的案例的更多相关文章

  1. 应用程序有bug崩溃重启的案例2

    ------解决思路----------------------另外做一个服务或者程序定时监控系统进程.程序奔溃的话,都会在入口函数出现异常处理一下winform可以有两个事件来捕获主线程异常和线程异 ...

  2. 微信小程序计算器Bug版=-=(笔记)

    微信小程序计算器BUG版本 无APPID的测试号登录,先在app.json中更改路径,以及修改头部信息. 首先一个输入框字段用{{screenData}} 功能可以退格,清屏,正负号,正常操作加减乘除 ...

  3. python程序爬虫总是崩溃

    写的一个爬虫程序,主要用到以下库.但是伴随着代码增多,功能增多.经常性的程序崩溃现象,逐渐显现. pyqt5_5.8.2,requests.get,selenium+chorme,threading. ...

  4. 【iOS】iOS 调试快速定位程序在哪崩溃

    iOS 开发过程中经常遇到程序崩溃.快速定位程序在哪崩溃的步骤如下: 1. 2. 3. 这样设置后,程序崩溃时会定位到崩溃的语句,如下: 原文链接:iOS开发何如在调试的时候轻松找到程序在哪里崩溃

  5. 微信小程序 TypeScript bug

    微信小程序 TypeScript bug 执行自定义预览前预处理命令失败! internal/modules/cjs/loader.js:584 throw err; ^ Error: Cannot ...

  6. N3K异常重启(案例)

    在实际的情况下,有时候会遇到设备无故重启的问题,这个时候,我们需要判断一下重启的根本原因是什么,是否有规避的方法等. 这里记录了几个N3K异常重启的问题. 案例1: 设备型号:N3K-C3048TP- ...

  7. java:如何让程序按要求自行重启?

    正文开始前的废话: 这里的程序即包括b/s的web application,也包括standalone的类c/s的java application.   为什么要自我重启?   场景1:分布式环境中, ...

  8. mfc封装cef浏览器 关闭整个窗口程序得时候又重启mfc 应用的程序

    最近使用mfc 做了一个cef得浏览器 多标签得.当使用这个封装得浏览器一段时间之后(超过1分钟2分钟) 当关闭封装得浏览器整个窗体 x得时候,整个窗体又重新弹了出来. 大概现象就是一个exe程序你杀 ...

  9. 非IT行业大企程序员讲述MIS系统开发案例

      雪莉叹了一口气,调整了一下被汗水濡湿的刘海,然后向后靠在办公椅中,伸手在电脑键盘上输入了一些内容, 最后拿起印刷着房地产广告的扇子,边扇风边等待着.   她的工位在办公室的东侧角落,侧靠着窗.此时 ...

随机推荐

  1. BZOJ3526 [Poi2014]Card 【线段树】

    题目链接 BZOJ3526 题解 思来想去,发现很显然可以用线段树维护 每个区间保存所有合法方案的左右端点[当左端点一定是,右端点当然存最小的那个就行了] 这么整的数,\(\frac{1}{1000} ...

  2. 【BZOJ4889】[Tjoi2017]不勤劳的图书管理员 分块+树状数组

    [BZOJ4889][Tjoi2017]不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让 ...

  3. HDU3068:最长回文(Manacher模板)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. apt-get update的hit和ign含义

    How do Ign and Hit affect apt-get update? From what I can see in the apt source code, "Ign" ...

  5. xml 通过正则抓取字段

    $str = '<xml> <appid><![CDATA[wxd49ea66070209a6e]]></appid> <bank_type> ...

  6. async/await 里的并行和串行

    我们在使用 async/await 语法时,有时会这样用: function getName () { return new Promise((resolve, reject)=>{ setTi ...

  7. windows版本redis下载安装

    官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 在官方下载网页中有一个window版本的说明,说redis官方没有wi ...

  8. asp:DropDownList与asp:DataList的联合使用

    情况:当在asp:DropDownLis点击选取其中的一个值来响应datalist的值. <form id="form1" runat="server"& ...

  9. 牛客多校第五场-D-inv

    链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...

  10. HDU 2154 跳舞毯 | DP | 递推 | 规律

    Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...