微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#定时执行

 代码

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

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

        public Form1()
        {
            InitializeComponent();
        }

        void button1_Click(object sender,EventArgs e)
        {
            System.Windows.Forms.Timer timer =  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();
                    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、这只是个测试程序,实际使用需要根据实际情况改造一下,比如定时到分钟就可以了,从配置文件中读取配置的定时时间,然后按程序中的方法判断有没有到这个时间。

测试日志截图:

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐