C#计时器,用system.Timers命名空间下的Timer类,使用Elapsed事件另开一个线程

1、首先初始化变量

        //定义Timer类变量
        long TimeCount = 0;
        static int interval = 1000;
        System.Timers.Timer Mytimer = new System.Timers.Timer(interval);
        //定义委托
        public delegate void SetControlValue(long value);

2、在窗体加载函数中添加如下代码

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置重复计时
            Mytimer.AutoReset = true;
            //设置执行System.Timers.Timer.Elapsed事件
            Mytimer.Elapsed += new System.Timers.ElapsedEventHandler(Mytimer_Tick);
            Mytimer.Start();
        }

Mytimer.AutoReset:true重复计时,false计时一次

 Mytimer.Elapsed:触发函数,当计时到达指定时间时,触发对应函数,例如本例中时间到达一秒时,触发Mytimer_Tick()函数

 Mytimer.Start():打开计时器

3、写触发函数

        private void Mytimer_Tick(object sender, EventArgs e)
        {
            this.Invoke(new SetControlValue(ShowTime), TimeCount);
            TimeCount++;
        }
        /// <summary>
        /// 给与一个秒数,显示时间
        /// </summary>
        /// <param name="t"></param>
        private void ShowTime(long t)
        {
            TimeSpan temp = new TimeSpan(0, 0, (int)t);
            Time_Count.Text = string.Format("{0:00}:{1:00}:{2:00}", temp.Hours, temp.Minutes, temp.Seconds);
        }

本例中当时间到1s时,Elapsed触发Mytimer_Tick函数,Mytimer_Tick触发ShowTime函数,ShowTime功能为显示文本框内容。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`System.Timers.Timer` 和 `System.Threading.Timer` 都是 .NET Framework 中的计时器,它们用于在一定的时间间隔后触发某个事件。但是,它们的使用方式略有不同。 为了避免产生分歧,建议使用 `using` 语句来使用这两个。具体做法如下: ```csharp using System; using System.Timers; using System.Threading; namespace MyNamespace { class MyClass { private Timer timer1; private Timer timer2; public MyClass() { // 使用 System.Timers.Timer timer1 = new System.Timers.Timer(1000); timer1.Elapsed += OnTimer1Elapsed; // 使用 System.Threading.Timer timer2 = new System.Threading.Timer(OnTimer2Elapsed, null, 1000, Timeout.Infinite); } public void Start() { timer1.Start(); timer2.Change(0, Timeout.Infinite); } public void Stop() { timer1.Stop(); timer2.Change(Timeout.Infinite, Timeout.Infinite); } private void OnTimer1Elapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("System.Timers.Timer elapsed"); } private void OnTimer2Elapsed(object state) { Console.WriteLine("System.Threading.Timer elapsed"); } } } ``` 在上述代码中,我们使用 `using` 语句引入了 `System.Timers` 和 `System.Threading` 命名空间,分别创建了一个 `System.Timers.Timer` 对象和一个 `System.Threading.Timer` 对象,并分别注册了它们的 `Elapsed` 事件处理程序。在 `Start` 和 `Stop` 方法中,我们分别启动和停止了这两个计时器。同时,我们还分别实现了 `OnTimer1Elapsed` 和 `OnTimer2Elapsed` 方法,用于处理计时器的 `Elapsed` 事件。 需要注意的是,`System.Timers.Timer` 与 `System.Threading.Timer` 的默认行为略有不同,因此在使用时需要仔细阅读官方文档,以确保正确使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sumzeek丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值