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 ...
随机推荐
- python之字串
python字串声明: 单引('), 双引("), 三引(''' 或 """"). python字串前缀: r表示原生字串, 字串内容: (1)不能包 ...
- CHROME下去掉保存密码后输入框变成黄色背景样式
之前没遇到过这种情况,现在打开这个页面后,手机号和密码都已经输入了,而且还显示的是黄色背景,清了下cookie,没有解决问题.请教了下大神,先把方法整理到这儿. 用代码审查看了input样式有如下样式 ...
- (转)c指针
转自:http://www.cnblogs.com/wchhuangya/archive/2009/12/24/1631121.html 这两天开始搞BREW了,用的是C的语法.上学时学过的C都还给学 ...
- python学习小结2:if和while控制语句
if语句 if语句中,代码块是按缩进的空格数量来判断的,也就是说空格数量一致的相邻行会被当作一个代码块,当if的条件成立的时候它就会得到执行. x = 100 if x > 50: print ...
- win7 telnet命令无法使用
很多做网络测试的同学发现安装win7后,无法使用telnet命令了,提示“telnet不是内部或外部命令,也不是可运行的程序”,但是很需要在win7中使用telnet工具,怎么办? 首先你要要确认你的 ...
- ADT eclipse打开时出现Error: Error parsing C:\Users\admin*\.android\devices.xml
Error: Error parsing C:\Users\admin*\.android\devices.xml 在ADT eclipse打开项目的时候出现此提示,但是又不影响使用. 原因:之前安 ...
- Oracle 删除表分区
删除表分区(drop partition) 删除表分区包含两种操作,分别是: Ø 删除分区:alter table [tbname] drop partition [ptname] UPDA ...
- Mongo:将查询结果转换为自定义类
1.自定义类 public class MyClass { public string Name { get; set; } public int Corners { get; set; } } 2. ...
- Maven开源中国镜像
mirrors> <mirror> <id>nexus-osc</id> <mirrorOf>central</mirrorOf> ...
- linux源代码阅读笔记 fork和execve的区别
1. man exec就可以知到: The exec() family of functions replaces the current process image with a new proce ...