Winfrom 配置异常重启监听

15 篇文章 0 订阅
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);
            }
        }
    }
}
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值