zoukankan      html  css  js  c++  java
  • 在catch中捕获了异常后重启应用程序

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Diagnostics;
     
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private List<string> strs = null;
     
            private void Form1_Load(object sender, EventArgs e)
            {
                strs = new List<string>();
            }
     
            private void timer1_Tick(object sender, EventArgs e)
            {
                try
                {
                    int num = int.Parse(lblSecondCount.Text);
                    num++;
                    strs.Add("a");
                    lblSecondCount.Text = num.ToString();
                    if (num == 5)
                    {
                        timer1.Enabled = false;
                        throw new Exception("5秒了,抛出异常!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    MessageBox.Show("即将重新启动应用程序!");
                    this.Close();
                    
                    /* 以下两种方式都可以重启应用程序 */
                    
                    // 方式一
                    Restart();
     
                    // 方式二
                    Run2();
                }
            }
     
            private void Restart()
            {
                Thread thtmp = new Thread(new ParameterizedThreadStart(Run1));
                object appName = Application.ExecutablePath;
                Thread.Sleep(2000);
                thtmp.Start(appName);
            }
     
            private void Run1(Object obj)
            {
                Process ps = new Process();
                ps.StartInfo.FileName = obj.ToString();
                ps.Start();
            }
     
            private void Run2()
            {
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.WorkingDirectory = Application.StartupPath;
                psi.FileName = Application.ExecutablePath;
                psi.Arguments = "";
                try
                {
                    Process.Start(psi);
                }
                catch (System.ComponentModel.Win32Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }
    }
  • 相关阅读:
    解决bash: less: command not found
    IDEA-相关插件使用
    如何理解多租户架构?
    mybatis自动生成model、dao及对应的mapper.xml文件
    IDEA设置提示生成序列化ID
    [DUBBO] qos-server can not bind localhost:22222错误解决
    @NotNull,@NotEmpty,@NotBlank区别
    (三)IDEA使用,功能面板
    PHP实现自己活了多少岁
    使用PHP函数输出前一天的时间和后一天的时间
  • 原文地址:https://www.cnblogs.com/SkySoot/p/2494505.html
Copyright © 2011-2022 走看看