您的足迹:首页 > 未分类 >C#一个异常重启winform程序

C#一个异常重启winform程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReStartManager
{
    public partial class StartForm : Form
    {
        public StartForm()
        {
            InitializeComponent();
        }
        Timer timer;
        private void StartForm_Load(object sender, EventArgs e)
        {
            try
            {
                DateTime NowTime = DateTime.Now;
                bool IsReturn = false;
                ///配置文件 重启次数+时间  固定每天最多只能重启3次
                string config = ReadTxt.GetConfig();
                string writeTxt = "";
                if (String.IsNullOrEmpty(config))
                {
                    writeTxt = "1," + NowTime.ToString();
                }
                else
                {
                    string[] value = config.Split(',');
                    int count = Convert.ToInt32(value[0]);
                    DateTime time = DateTime.Parse(value[1]);

                    if (count >= 3)
                    {
                        writeTxt = "";
                        IsReturn = true;
                    }
                    else
                    {
                        if (NowTime.Year == time.Year && NowTime.Month == time.Month && NowTime.Day == time.Day)
                        {
                            writeTxt = (count + 1) + "," + DateTime.Now.ToString();
                        }
                        else
                        {
                            writeTxt = "1," + NowTime.ToString();
                        }
                    }
                }
                ReadTxt.WriteTxt(writeTxt);

                //重启次数大于三次 不再重启
                if (IsReturn)
                {
                    Application.Exit();
                    return;
                }
                timer = new Timer();
                timer.Interval = 3000;
                timer.Tick += Timer_Tick;
                timer.Start();
            }
            catch (Exception ex)
            {
                LoggingHelper.Error("StartForm_Load异常" + ex.Message);
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            try
            {
                timer.Stop();
                ReStart();
                Application.Exit();
            }
            catch (Exception ex)
            {
                LoggingHelper.Error("Timer_Tick异常" + ex.Message);
            }
        }

        /// <summary>
        /// 重启
        /// </summary>
        private void ReStart()
        {
            try
            {
                string processPath = AppDomain.CurrentDomain.BaseDirectory;
                Process winHello = new Process();

                winHello.StartInfo.UseShellExecute = true;
                winHello.StartInfo.FileName = processPath + "XXX.exe";
                winHello.StartInfo.CreateNoWindow = true;
                winHello.Start();

                LoggingHelper.Info("重启成功");
            }
            catch (Exception ex)
            {
                LoggingHelper.Error("重启失败" + ex.Message);
            }
        }
    }
}
本博客所有文章如无特别注明均为原创。作者:cc复制或转载请以超链接形式注明转自 我的博客
原文地址《C#一个异常重启winform程序

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)