c# Timer event 定时器事件

1 简介

实现定时器事件,每隔一个时间后触发事件。
需要注意常用的有两个计时器,一个是System.Timers.Timer,另一个常用的是 System.Windows.Forms.Timer。这两个定时器有很大的形似性。但是要注意,如果是Forms程序,用到控件时要选Forms.Timer,否则更新控件时会出现cross multithread问题,不是一个线程。这里以System.Timers.Timer做原理解说,后面示例代码会贴出两个Timer的。

2.原理

2.1 命名空间

using System.Timers;

2.2 声明定时器

private Timer aTimer;

2.3 实例化定时器并设置属性

 // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

2.4 事件函数

时间函数的函数名要与注册事件语句中的函数名要一致。

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }

3.代码示例

3.1 System.Timers.Timer实例

using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM

3.2 System.Windows.Forms.Timer定时器实例

public class Class1 {
    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    static int alarmCounter = 1;
    static bool exitFlag = false;
 
    // This is the method to run when the timer is raised.
    private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) {
       myTimer.Stop();
 
       // Displays a message box asking whether to continue running the timer.
       if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, 
          MessageBoxButtons.YesNo) == DialogResult.Yes) {
          // Restarts the timer and increments the counter.
          alarmCounter +=1;
          myTimer.Enabled = true;
       }
       else {
          // Stops the timer.
          exitFlag = true;
       }
    }
 
    public static int Main() {
       /* Adds the event and the event handler for the method that will 
          process the timer event to the timer. */
       myTimer.Tick += new EventHandler(TimerEventProcessor);
 
       // Sets the timer interval to 5 seconds.
       myTimer.Interval = 5000;
       myTimer.Start();
 
       // Runs the timer, and raises the event.
       while(exitFlag == false) {
          // Processes all the events in the queue.
          Application.DoEvents();
       }
    return 0;
    }
 }
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: timer定时器是一种计时工具,常用于需要定时执行某些任务的场景中。它能够在设定的时间到达后触发某个事件或执行某段代码。 timer定时器的使用非常灵活,可以根据需求设置不同的时间间隔和执行次数。常见的定时器有单次定时器和循环定时器两种。 单次定时器可以在指定的时间间隔后执行一次任务,并且只执行一次。例如,我们可以设置一个单次定时器,在5秒后触发某个函数执行。 循环定时器可以按照设定的时间间隔不断重复执行任务。例如,我们可以设置一个循环定时器,每隔1分钟触发某个函数执行。 timer定时器不仅可以用于编程语言中,也可以用在各种电子设备中。比如,我们可以在手机上设置一个定时器,用来提醒我们在某个时间点做某件事情,或者在烹饪过程中使用定时器来控制食物的烹饪时间。 总之,timer定时器是一种非常实用的工具,可以在需要定时执行任务的场景中发挥重要作用。无论是在编程中还是在日常生活中,我们都可以利用timer定时器来提高效率和方便操作。 ### 回答2: timer定时器是一种用于控制时间间隔的设备或工具。它可以在给定的时间间隔内执行特定的任务或操作。 timer定时器通常用于各种计时和定时操作,例如在游戏中实现角色的移动、动画效果的切换,或者定时播放音乐等。 在编程中,我们可以使用timer定时器来实现事件的定时触发。通过设置一个特定的时间间隔和一个回调函数,当时间到达设定的时间间隔时,定时器将会触发回调函数,并执行其中的代码。 timer定时器可以在编程语言中以不同形式实现,例如在C语言中,我们可以使用标准库中的函数来创建和使用定时器。在C语言中,我们通常需要定义一个回调函数,用于实现定时触发时需要执行的操作,然后使用定时器函数来设置时间间隔和回调函数。 定时器的使用可以帮助我们在程序中实现各种时间相关的任务,提高程序的可靠性和运行效率。但是需要注意的是,在使用定时器时需要合理设置时间间隔,避免频繁触发定时器导致系统负荷过大和性能下降。 总结起来,timer定时器是一种用于控制时间间隔的设备或工具,它在编程中可以用来实现各种时间相关的操作。通过设置时间间隔和回调函数,我们可以实现定时触发并执行相应的代码。 ### 回答3: timer定时器是一种用来测量或控制时间间隔的设备或工具。它通常用于在特定时间间隔内执行一些操作或发出信号。 timer定时器有广泛的应用领域。在家庭生活中,我们可以使用定时器来设置闹钟,提醒我们起床、上班、做饭等。在厨房中,我们可以设置烤箱定时器来保证食物在正确定时烹饪,防止过度或不足。 在工业领域,timer定时器被广泛应用于自动化系统中。它可以用来控制设备的运行时间,例如定时关闭空调或照明设备,节约能源。定时器还可以用于控制机器的运转周期,在特定时间间隔内启动或停止机器,提高生产效率。 在计算机编程中,timer定时器也是一个重要的概念。它可以用来控制代码的执行时间,例如设置一个定时器在特定时间间隔内反复执行某个函数或代码块。这在游戏开发中很常见,用来实现动画效果或定期更新游戏状态。 总之,timer定时器在我们的生活和工作中扮演着重要的角色。无论是作为一个简单的时间管理工具还是一个复杂的控制设备,它都能够帮助我们提高效率、节约时间,并实现各种不同的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值