C#上位机1ms级高精度定时任务

precisiontimer

安装扩展包

添加引用

完整代码

cs 复制代码
using PrecisionTiming;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //参考视频
        //https://www.bilibili.com/video/BV1fd4y1v7dK/?spm_id_from=333.999.0.0&vd_source=e821a225c7ba4a7b85e5aa6d013ac92e
        PrecisionTimer timer = new PrecisionTimer();
        private void button1_Click(object sender, EventArgs e)
        {
            //timer.SetInterval(1);
            //timer.SetPeriodic(1);
            //timer
            timer.SetInterval(5);
            //方法1
            //timer.SetAction(new Action(() => 
            //{
            //    Console.WriteLine(DateTime.Now.Millisecond.ToString());
            //}));
            //方法2
            //Action showMethod = print;
            //timer.SetAction(showMethod);
            //方法3            
            timer.SetAction(print);

            timer.Start();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }
        private void print()
        {
            Console.WriteLine(DateTime.Now.Millisecond.ToString());
        }
    }
}

运行效果

资源链接

【免费】c#高精度毫秒定时器,实现windows高精度定时器功能资源-CSDN文库https://download.csdn.net/download/anlog/89290452

参考链接

高精度定时器的使用以及实现_c# precisiontimer-CSDN博客https://blog.csdn.net/i78i845/article/details/130220156

C#上位机1ms级高精度定时任务讲解_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1fd4y1v7dK/?spm_id_from=333.337.search-card.all.click&vd_source=e821a225c7ba4a7b85e5aa6d013ac92e

特此记录

anlog

2024年5月10日

相关推荐
anlog6 个月前
C#实现纳秒级的计时器功能
高精度·定时器·高精度定时器