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

  • 相关阅读:
    禁止select下拉框的其中某个选择项不能被选择
    jQuery 增加 删除 修改select option
    jquery ajax后台向前台传list 前台用jquery $.each遍历list
    转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)
    C#性能优化实践 资料整理
    MySql 优化 网上资料
    第06组 Alpha冲刺(4/6)
    第06组 Alpha冲刺(3/6)
    第06组 Alpha冲刺(2/6)
    第06组 Alpha冲刺(1/6)
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/8084563.html
Copyright © 2011-2022 走看看