Winfrom 配置异常重启监听

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

        /// 
        /// 重启
        /// 
        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);
            }
        }
    }
}

你可能感兴趣的:(C#,winform,c#,开发语言)