C#中的三种定时计时器Timer用法介绍

作者:.NET开发菜鸟 时间:2023-06-09 13:31:13 

在.NET中有三种计时器:

  • 1、System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet。Timer控件只有绑定了Tick事件和设置Enabled=True后才会自动计时,停止计时可以用Stop()方法控制,通过Stop()停止之后,如果想重新计时,可以用Start()方法来启动计时器。Timer控件和它所在的Form属于同一个线程;

  • 2、System.Timers命名空间下的Timer类。System.Timers.Timer类:定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法来启动计时,通过Stop()方法或者Enable=false停止计时。AutoReset属性设置是否重复计时(设置为false只执行一次,设置为true可以多次执行)。Elapsed事件绑定相当于另开了一个线程,也就是说在Elapsed绑定的事件里不能访问其它线程里的控件(需要定义委托,通过Invoke调用委托访问其它线程里面的控件)。

  • 3、System.Threading.Timer类。定义该类时,通过构造函数进行初始化。

在上面所述的三种计时器中,第一种计时器和它所在的Form处于同一个线程,因此执行的效率不高;而第二种和第三种计时器执行的方法都是新开一个线程,所以执行效率比第一种计时器要好,因此在选择计时器时,建议使用第二种和第三种。

下面是三种定时器使用的例子:

1、Timer控件

设计界面:

C#中的三种定时计时器Timer用法介绍

后台代码:

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 TimerDemo
{
   public partial class FrmMain : Form
   {
       //定义全局变量
       public int currentCount = 0;
       public FrmMain()
       {
           InitializeComponent();
       }

private void FrmMain_Load(object sender, EventArgs e)
       {
           //设置Timer控件可用
           this.timer.Enabled = true;
           //设置时间间隔(毫秒为单位)
           this.timer.Interval = 1000;
       }

private void timer_Tick(object sender, EventArgs e)
       {
           currentCount += 1;
           this.txt_Count.Text = currentCount.ToString().Trim();
       }

private void btn_Start_Click(object sender, EventArgs e)
       {
           //开始计时
           this.timer.Start();
       }

private void btn_Stop_Click(object sender, EventArgs e)
       {
           //停止计时
           this.timer.Stop();
       }
   }
}

2、System.Timers.Timer

设计界面:

C#中的三种定时计时器Timer用法介绍

后台代码:

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 TimersTimer
{
   public partial class FrmMain : Form
   {
       //定义全局变量
       public int currentCount = 0;
       //定义Timer类
       System.Timers.Timer timer;
       //定义委托
       public delegate void SetControlValue(string value);

public FrmMain()
       {
           InitializeComponent();
       }

private void Form1_Load(object sender, EventArgs e)
       {
           InitTimer();
       }

/// <summary>
       /// 初始化Timer控件
       /// </summary>
       private void InitTimer()
       {
           //设置定时间隔(毫秒为单位)
           int interval = 1000;
           timer = new System.Timers.Timer(interval);
           //设置执行一次(false)还是一直执行(true)
           timer.AutoReset = true;
           //设置是否执行System.Timers.Timer.Elapsed事件
           timer.Enabled = true;
           //绑定Elapsed事件
           timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
       }

/// <summary>
       /// Timer类执行定时到点事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
       {
           try
           {
               currentCount += 1;
               this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString());
           }
           catch (Exception ex)
           {
               MessageBox.Show("执行定时到点事件失败:" + ex.Message);
           }
       }

/// <summary>
       /// 设置文本框的值
       /// </summary>
       /// <param name="strValue"></param>
       private void SetTextBoxText(string strValue)
       {
           this.txt_Count.Text = this.currentCount.ToString().Trim();
       }

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

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

3、System.Threading.Timer

设计界面:

C#中的三种定时计时器Timer用法介绍

后台代码:

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;
using System.Threading;

namespace Threading.Timer
{
   public partial class FrmMain : Form
   {
       //定义全局变量
       public int currentCount = 0;
       //定义Timer类
       System.Threading.Timer threadTimer;
       //定义委托
       public delegate void SetControlValue(object value);

public FrmMain()
       {
           InitializeComponent();
       }

private void FrmMain_Load(object sender, EventArgs e)
       {
           InitTimer();
       }

/// <summary>
       /// 初始化Timer类
       /// </summary>
       private void InitTimer()
       {
           threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);
       }

/// <summary>
       /// 定时到点执行的事件
       /// </summary>
       /// <param name="value"></param>
       private void TimerUp(object value)
       {
           currentCount += 1;
           this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);
       }

/// <summary>
       /// 给文本框赋值
       /// </summary>
       /// <param name="value"></param>
       private void SetTextBoxValue(object value)
       {
           this.txt_Count.Text = value.ToString();
       }

/// <summary>
       /// 开始
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void btn_Start_Click(object sender, EventArgs e)
       {
           //立即开始计时,时间间隔1000毫秒
           threadTimer.Change(0, 1000);
       }

/// <summary>
       /// 停止
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void btn_Stop_Click(object sender, EventArgs e)
       {
           //停止计时
           threadTimer.Change(Timeout.Infinite, 1000);
       }
   }
}

代码下载链接:点此下载

来源:https://www.cnblogs.com/dotnet261010/p/7113523.html

标签:C#,定时,计时器,Timer
0
投稿

猜你喜欢

  • 常见的排序算法,一篇就够了

    2022-06-16 06:56:34
  • Kotlin语言使用WebView示例介绍

    2021-10-14 05:56:21
  • IDEA 使用mybatis插件Free Mybatis plugin的步骤(推荐)

    2022-12-05 02:01:03
  • C#中XmlTextWriter读写xml文件详细介绍

    2022-01-26 05:13:48
  • java设计模式-单例模式实现方法详解

    2022-02-15 13:56:01
  • android播放器实现歌词显示功能

    2021-10-27 13:44:37
  • C#十五子游戏编写代码

    2023-06-13 07:33:22
  • Java原生HttpClient的使用详解

    2022-06-04 16:29:49
  • C#多线程系列之线程通知

    2023-09-13 17:08:03
  • 25个最好的免费Eclipse插件

    2021-09-21 10:56:24
  • 2022 最新 IntelliJ IDEA 详细配置步骤演示(推荐)

    2021-11-20 21:05:48
  • JAVA中实现链式操作(方法链)的简单例子

    2022-12-16 00:54:50
  • Android实现卫星菜单效果

    2021-12-12 23:44:28
  • 全网最详细Hutool工具详解

    2023-05-04 06:21:53
  • C#使用linq对数组进行筛选排序的方法

    2023-12-06 06:21:21
  • C#中的不可变数据类型介绍(不可变对象、不可变集合)

    2022-06-13 19:08:33
  • spring boot线上日志级别动态调整的配置步骤

    2022-09-19 01:57:45
  • C# Ini文件操作实例

    2022-12-31 00:37:45
  • Java中闭包简单代码示例

    2023-11-08 23:09:48
  • Android自定义listview布局实现上拉加载下拉刷新功能

    2023-05-12 23:27:28
  • asp之家 软件编程 m.aspxhome.com