zoukankan      html  css  js  c++  java
  • 应用程序有bug崩溃重启的案例

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

  • 相关阅读:
    [thinkphp] 是如何输出一个页面的
    [thinkphp] 获取根目录绝对路径
    onethink 插件模板定位
    win7 安全模式开启声音
    百度贴吧楼层评论地址
    第一天问题
    [php] 解析JSON字符串
    NDK编译时两 .so之间调用问题
    CDN问题积累
    C++模板特化
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/8084563.html
Copyright © 2011-2022 走看看