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),我们将立即处理。