C#定时执行

 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common.Utils;

namespace test
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 记录上次执行时间的文件路径
        /// </summary>
        private string lastRunTimePath = Path.Combine(Application.StartupPath, "lastRunTime.txt");
        /// <summary>
        /// 上次执行时间,精确到秒
        /// </summary>
        private string lastRunTime
        {
            get
            {
                if (File.Exists(lastRunTimePath))
                {
                    using (StreamReader sr = new StreamReader(lastRunTimePath))
                    {
                        string result = sr.ReadToEnd();
                        sr.Close();
                        return result;
                    }
                }
                return null;
            }
            set
            {
                if (!File.Exists(lastRunTimePath))
                {
                    File.Create(lastRunTimePath);
                }
                using (StreamWriter sw = new StreamWriter(lastRunTimePath, false))
                {
                    sw.Write(value);
                    sw.Close();
                }
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 50; //定时执行的时间精确到秒,那么Timer的时间间隔要小于1秒
            timer.Tick += new EventHandler((a, b) =>
            {
                DateTime now = DateTime.Now;
                if (now.ToString("yyyy-MM-dd HH:mm:ss") != lastRunTime) //如果相等则说明已经执行过了
                {
                    lastRunTime = now.ToString("yyyy-MM-dd HH:mm:ss");
                    LogUtil.Log(now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 时间已到,执行任务");

                    //TODO:在这里执行任务
                }
            });
            timer.Start();
        }

    }
}
View Code

说明:

1、为什么要将lastRunTime存到文件中?

    这是为了程序重起的时候,仍能正确判断时间,不会重复执行。

2、和使用Windows定时任务的方式实现有什么区别?

    这也是这种实现方式的缺点,精度不够高,如果定时执行定时到分钟,一般精度也只到分钟,可以应用于对于定时时间精确程度不高的定时任务。虽然减小Interval可以提高定时精度,但耗费CPU资源。

3、为什么要这样实现,直接判断时分秒不就行了吗?

    程序运行需要时间,哪怕这个时间非常短,会导致跳过某个时间,例如Interval设置为1000,也就是1秒,13时50分29.999秒执行了一次,下一次判断的时候到了13时50分31.001秒,跳过了13时50分30秒,如果秒数用等于判断,就不会执行了。

4、这只是个测试程序,实际使用需要根据实际情况改造一下,比如定时到分钟就可以了,从配置文件中读取配置的定时时间,然后按程序中的方法判断有没有到这个时间。

测试日志截图:

原文地址:https://www.cnblogs.com/s0611163/p/6126808.html