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控件
设计界面:
后台代码:
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
设计界面:
后台代码:
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
设计界面:
后台代码:
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


猜你喜欢
- 如果想你写的程序随系统开机一起启动的话,那么你可以照下面这个方法来做。 RunWhenStart(false, Applicati
- 一、ConcurrentLinkedQueue介绍并编程中,一般需要用到安全的队列,如果要自己实现安全队列,可以使用2种方式:方式1:加锁,
- 水流波动的波形都是三角波,曲线是正余弦曲线,但是Android中没有提供绘制正余弦曲线的API,好在Pa
- 模拟登陆的原理很简单,就是发送一个Http 请求服务器获得响应,然后客户端获取到cookie即可实现模拟登陆,比如一些抢票软件的原理无非也是
- Android在启动模拟器AVD时,出现下面的异常:“Failed to allocate memory: 8”,怎么办?此错误是我们在允许
- 以前,使用github(git)结合 IntelliJ IDEA ,可以把自己本地的测试代码,使用github网站,添加到版本管理。这样就可
- Java原生SPI面向接口编程+策略模式实现建立接口Robotpublic interface Robot { /
- 本文实例为大家分享了Android空心圆及层叠效果的具体代码,供大家参考,具体内容如下package com.bwei.test.zidin
- 今天介绍一下,我在项目开发过程中,实现状态栏和虚拟按键背景颜色变化的方法,实现方式是,通过隐藏系统的状态栏和虚拟按键的背景,实现图片和背景显
- 需求分析需求一:图片列表查询,从后台返回数据,将数据展示在页面上需求二:新增图片,将新增图书的数据传递到后台,并在控制台打印说明:此次案例的
- 和大家一起分享一下学习经验,如何实现Android文件下载进度显示功能,希望对广大初学者有帮助。先上效果图:上方的蓝色进度条,会根据文件下载
- Feign其他功能-超时设置Feign 底层依赖于 Ribbon 实现负载均衡和远程调用。Ribbon默认1秒超时。超时配置:ribbon:
- Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置:在JAVA程序上右键--> Ru
- 本文章向大家介绍JAVA爬取天天基金网数据,主要包括JAVA爬取天天基金网数据使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参
- 一、概念 1. 为了能让程序操作数据库,对数据库中的表进行操作,每一种数据库都会提供一套连接和操作该数据库的驱动,而且每种数据库
- 下面由我来给大家展示用spring aop实现 * 的例子(电脑打印)下面就看一下具体的代码:先定义一个打印机的接口package aop
- 本文主要讲解利用android中Matrix控制图形的旋转缩放移动,具体参见一下代码:/** * 使用矩阵控制图片移动、缩放、旋
- PS:用了一下个推.感觉实现第三方应用的推送功能还是比较简单的.官方文档写的也非常的明确.学习内容:1.使用个推实现第三方应用的推送.所有的
- 前面的文章已经实现相关的布局,本文接着进行相关的功能实现读取系统联系人当点击“选择联系人”按钮后,弹出联系人列表,读取系统联系人分如下几个步
- java通过IP解析地理位置在项目开发中,需要在登录日志或者操作日志中记录客户端ip所在的地理位置。目前根据ip定位地理位置的第三方api有