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 ...
随机推荐
- Aspose 数字和日期 设置
Microsoft Excel一个非常强大的功能就是使客户可以设置数字和日期的显示格式,众所周知数字可以显示为不同的值格式,包含:小数.货币.百分数.分数.账面价值等,同样地Aspose.Cells也 ...
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 1654 方程的解 - Wikioi
题目描述 Description佳佳碰到了一个难题,请你来帮忙解决.对于不定方程a1+a2+… +ak-1 +ak=g(x),其中k≥2且k ∈ N*,x是正整数,g(x) =xx mod 1000( ...
- Nginx 301重定向域名
为何要使用301重定向 在网站建设中需要网页重定向的情况很多:如网页目录结构变动,网页重命名.网页的扩展名改变.网站域名改变等.如果不做重定向,用户的收藏和搜索引擎数据库中的旧地址只能让访客得到一个4 ...
- soap 路由
下面主要通过项目实例来具体阐述如何实现wse路由和一些项目开发中的细节.本人水平有限,有不对的地方,请朋友们不吝赐教. 在开始项目之前,先了解一下路由的概念,所谓"路由",是指把数 ...
- ECMALL目录结构设置与数据库表
[Ecmall]ECMALL目录结构设置与数据库表 最近在做ecmall的开发,ecmall在开源方面还有待进步啊,官方没有提供开发文档,也没有关于系统架构组织的贡献,使用者都要自己从0开始,官方 ...
- PHP7 扩展之自动化测试
在安装 PHP7 及各种扩展的过程中,如果你是用源码安装,会注意到在 make 成功之后总会有一句提示:Don't forget to run 'make test'. 这个 make test 就是 ...
- javascript分享到的源码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- SQLMap使用
http://www.freebuf.com/articles/web/29942.html http://sqlmap.org/ http://blog.csdn.net/zgyulongfei/a ...
- 架构探险——从零开始写Java Web框架》第二章照作
沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...