C# 定时器类实现1s定时器更新UI

1. 代码

//BaseTimer.cs

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace baseTimer
{
    //定义一个timer超时的委托
    public delegate void baseTimerDelegate(int cnt);

    
    class BaseTimer : IDisposable
    {
        private bool disposed = false;        
        private System.Threading.Timer timer1;
        private int period = 0;
        private int cnt = 0;
        private readonly object lockObject = new object();
        private bool isTimerRunning = false;
        private baseTimerDelegate timeoutDelegate;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    timer1?.Dispose();
                    timer1 = null;
                }
                disposed = true;
            }
        }

        public BaseTimer(baseTimerDelegate Delegate1,int period1)
        {
            period = period1;
            timeoutDelegate = Delegate1;
            timer1 = new System.Threading.Timer(TimerStepCallBack, null, Timeout.Infinite, period);
        }
        private void TimerStepCallBack(Object stateInfo)
        {
            cnt++;
            timeoutDelegate(cnt);
        }

        /// <summary>
        /// 启动定时器,每秒中断一次
        /// </summary>
        public void Start(int ms)
        {
            lock (lockObject)
            {
                if (!isTimerRunning)
                {
                    isTimerRunning = true;
                    cnt = 0;
                    // 立即启动,每秒触发一次
                    period = ms;
                    timer1.Change(0, ms);
                }
            }
        }

        /// <summary>
        /// 停止定时器
        /// </summary>
        public void Stop()
        {
            lock (lockObject)
            {
                if (isTimerRunning)
                {
                    timer1.Change(Timeout.Infinite, period);
                    isTimerRunning = false;
                    cnt = 0;
                    timeoutDelegate(cnt);
                }
            }
        }
    }
}

//Form1.cs

csharp 复制代码
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;

namespace baseTimer
{
    public partial class Form1 : Form
    {
        //定义一个timer超时的委托
        private baseTimerDelegate baseTimerDelegate1;
        private BaseTimer baseTimer1;

        public Form1()
        {
            InitializeComponent();

            baseTimerDelegate1 = new baseTimerDelegate(TimerBaseInvoke);
            baseTimer1 = new BaseTimer(baseTimerDelegate1,1000);
            // 订阅窗体关闭事件
            this.FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            baseTimer1?.Dispose();
        }

        private void TimerBaseInvoke(int cnt)
        {
            Invoke((EventHandler)(delegate
            {
                textBox1.Text = cnt.ToString();
            }));
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            baseTimer1.Start(1000);
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            baseTimer1.Stop();
        }
    }
}

2 . 效果

相关推荐
用户2986985301417 小时前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户36674625267418 小时前
接口文档汇总 - 2.设备状态管理
c#
用户36674625267418 小时前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang2 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf5 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530145 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools6 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的6 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21886 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php