基本信息
源码名称:C# 高精度定时器 示例源码
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2018-07-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍

该例子实现了高精度定时器功能,最高定时精度为1ms,最小定时精度为1ms。


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

namespace timer
{
    public partial class Form1 : Form
    {
        MillisecondTimer timer;//最小定时时间1ms,新开一个线程执行任务
        //定义委托
        public delegate void SetControlValue(object value);

        public Form1()
        {
            InitializeComponent();
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new MillisecondTimer();
            timer.Tick  = sysTimer_Tick; //基于回调
            timer.Interval = 1; //当定时器启动时修改该值无效,需要先关掉定时器然后修改再重启
        }

        int count = 0;
        private void sysTimer_Tick(object sender, EventArgs e)
        {
            count  ;

            //textBox1.Text = count.ToString();
            try
            {
                this.Invoke(new SetControlValue(SetTextBoxValue), count.ToString());//当窗体关闭后可能发生异常
            }
            catch (Exception)
            {
                
            }

            if (count >= 10000)
            {
                count = 0;
                timer.Stop();
            }
        }

        private void SetTextBoxValue(object value)
        {
            this.textBox1.Text = value.ToString();
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            timer.Start();
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (timer != null)
                timer.Stop();
        }
    }
}