陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

一个异常重启winform程序

2018年4月8日 591点热度 0人点赞 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);
            }
        }
    }
}

 

标签: winform
最后更新:2021年4月2日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
js winform nginx C# python centos linux DevExpress
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
如何配置log4net,让日志分类记录到不同的日志文件中 docker打包python镜像全教程 python opencv剪切视频成多张图片 dynamic的操作 Naudio实现声卡采集麦克风采集+混音 一款采用MUI框架写的跨平台移动App应用

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS