C# 计时器的三种使用方法
在.net中有三种计时器,一是System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet;二是System.Timers命名空间下的Timer类。
Timer控件:Timer控件只有绑定了Tick事件,和设置Enabled=True后才会自动计时,停止计时可以用Stop()控制,通过Stop()停止之后,如果想重新计时,可以用Start()方法来启动计时器。Timer控件和它所在的Form属于同一个线程;
System.Timers.Timer类:定义一个System.Timers.Timer对象,绑定Elapsed事件,通过Start()方法启动计时,通过Stop()方法或者Enable=False停止计时。AutoReset属性设置是否重复计时。Elapsed事件绑定就相当另开了一个线程,也就是说在Elapsed绑定的事件里不能访问其它线程里的控件。
System.Threading.Timer:定义该类时,主要有四个参数。TimerCallBack,一个返回值为void,参数为object的委托,也是计时器执行的方法。Object state,计时器执行方法的的参数。 int dueTime,调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。
int Period,调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。
在这三种计时器中,第一种计时器和所在的Form处于同一个线程,因此执行的效率不高。而第二种和第三中计时器执行的方法都是新开一个线程,所以执行效率比第一种计时器要好。因此在使用计时器时,建议使用第二种和第三种。
下面是三中定时器使用的例子
1)Timer控件
public partial class Timer : Form
{
int count = 0;
public Timer()
{
InitializeComponent();
//timer控件可用
this.timer1.Enabled = true;
//设置timer控件的Tick事件触发的时间间隔
this.timer1.Interval = 1000;
//停止计时
this.timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
count += 1;
this.tbTimer.Text = count.ToString();
}
private void btStart_Click(object sender, EventArgs e)
{
//开始计时
this.timer1.Start();
}
private void btStop_Click(object sender, EventArgs e)
{
//停止计时
this.timer1.Stop();
}
}
2)System.Timers.Timer
public partial class Timer : Form
{
int count = 0;
private System.Timers.Timer timer = new System.Timers.Timer();
public Timer()
{
InitializeComponent();
//设置timer可用
timer.Enabled = true;
//设置timer
timer.Interval = 1000;
//设置是否重复计时,如果该属性设为False,则只执行timer_Elapsed方法一次。
timer.AutoReset = true;
timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
count += 1;
SetTB(count.ToString());
}
private void btStart_Click(object sender, EventArgs e)
{
timer.Start();
}
private void btStop_Click(object sender, EventArgs e)
{
timer.Stop();
}
private delegate void SetTBMethodInvok(string value);
private void SetTB(string value)
{
if (this.InvokeRequired)
{
this.Invoke(new SetTBMethodInvok(SetTB), value);
}
else
{
this.tbTimer.Text = value;
}
}
}
3) System.Threading.Timer
public partial class Timer : Form
{
int count = 0;
System.Threading.Timer timerThr;
private delegate void SetTBMethodInvoke(object state);
public Timer()
{
InitializeComponent();
//初始化一个计时器,一开始不计时,调用Callback的时间间隔是500毫秒
timerThr = new System.Threading.Timer(new TimerCallback(SetTB), null, Timeout.Infinite, 500);
}
public void SetTB(object value)
{
if (this.InvokeRequired)
{
this.Invoke(new SetTBMethodInvoke(SetTB), value);
}
else
{
count += 1;
this.tbTimer.Text = count.ToString();
}
}
private void btStart_Click(object sender, EventArgs e)
{
//开始计时
timerThr.Change(0, 500);
}
private void btStop_Click(object sender, EventArgs e)
{
//停止计时
timerThr.Change(Timeout.Infinite, 500);
}
}
C# 计时器的三种使用方法的更多相关文章
- javase-常用三种遍历方法
javase-常用三种遍历方法 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public ...
- JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- Jquery中each的三种遍历方法
Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...
- spring与mybatis三种整合方法
spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...
- C#使用DataSet Datatable更新数据库的三种实现方法
本文以实例形式讲述了使用DataSet Datatable更新数据库的三种实现方法,包括CommandBuilder 方法.DataAdapter 更新数据源以及使用sql语句更新.分享给大家供大家参 ...
- struts2拦截器interceptor的三种配置方法
1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...
- selenium webdriver三种等待方法
webdriver三种等待方法 1.使用WebDriverWait from selenium import webdriverfrom selenium.webdriver.common.by im ...
- ASP.NET文件上传的三种基本方法
ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录. <form i ...
随机推荐
- Java 包(package)详解
为了更好地组织类,Java提供了包机制,用于区别类名的命名空间. 包的作用 1 把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2 如同文件夹一样,包也采用了树形目录的存储方式.同一 ...
- [MVC] - 异步调用后台的常用方法。
1. 直接调用Action @Html.Action("GetTopArticle", "Home") 2. 通过url, 并用Jquery异步加载. < ...
- perl 脚本测试
原文地址: http://blog.csdn.net/johnny710vip/article/details/8905239 这是一篇关于perl脚本测试的总结性文章,其中提到了很多实用的 ...
- 怎么删除有外键约束的MySQL表中的数据
SET FOREIGN_KEY_CHECKS = 0 操作结束后 SET FOREIGN_KEY_CHECKS = 1
- C# 连接 Oracle 的几种方式[转]
本文转自:http://www.cnblogs.com/storys/archive/2013/03/06/2945914.html 一:通过System.Data.OracleClient(需要安装 ...
- HTML5网页制作教程:HTML5块级链接
网页制作Webjx文章简介:Web 标准中处处充满了打脸行为,这条规则现在已经失效了!在那篇文章发布一个月后,HTML5doctor 发表了 “Block-level” links in HTML5, ...
- 剑指offer--面试题8
题目:求旋转数组中的最小数字 以下为自己所写代码: #include "stdafx.h" #include <iostream> #include <excep ...
- DevOps 和技术债务偿还自动化
当企业想要迁移到一个 DevOps 模型时,经常需要偿还高等级的技术债务 说得更明确一点,机构往往陷入「技术债务的恶性循环」中,以至于任何迅速.敏捷的迁移方式都无法使用.这是技术债务中的希腊债务危机水 ...
- Test a ; vs Test a( ) ;
一. Test a(); Test a; //前提声明了Test类 前者声明一个返回值为Test,名为a的函数,后者声明了Test类的一个对象(把Test当成int) struct Test{ ...
- java核心技术记录之java术语
术语名 缩写 解释 Java Development Kit JDK 编写java程序的程序员使用的软件 Java Runtime Environment JRE 运行java程序的用户使用的软件 S ...