在.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# 计时器的三种使用方法的更多相关文章

  1. javase-常用三种遍历方法

    javase-常用三种遍历方法 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public ...

  2. JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  3. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  4. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  5. spring与mybatis三种整合方法

    spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...

  6. C#使用DataSet Datatable更新数据库的三种实现方法

    本文以实例形式讲述了使用DataSet Datatable更新数据库的三种实现方法,包括CommandBuilder 方法.DataAdapter 更新数据源以及使用sql语句更新.分享给大家供大家参 ...

  7. struts2拦截器interceptor的三种配置方法

    1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...

  8. selenium webdriver三种等待方法

    webdriver三种等待方法 1.使用WebDriverWait from selenium import webdriverfrom selenium.webdriver.common.by im ...

  9. ASP.NET文件上传的三种基本方法

    ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录. <form i ...

随机推荐

  1. z470 装黑苹果 10.92

    1.分两个区,一个是mac安装区,一个是镜像拷贝区. 2.把镜像压进去. 3.安装好系统. 4.把镜像区的 extent拷贝到安装好的系统盘里去. 5.安装驱动,网盘里有.还有系统也在网盘里. 6.声 ...

  2. NGUI3.5系列教程之 一些小功能的实现

    (一)可拖动窗体的实现: 1:添加一个Sprite为鼠标点击区域,改名为:DragSprite 2:给DragSprite添加Collider 3:给DragSprite添加Drag Object , ...

  3. iTween基础之Scale(缩放大小)

    一.基础介绍:二.基础属性 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50684392 一.基础介绍 ScaleTo:改变游戏对象的 ...

  4. 【狼窝乀野狼】Excel那些事儿

    在工作中我们常常遇到Excel表格,不管是数据的导入导出,还是财务统计什么都,都离不开Excel,Excel是我见过的最牛逼的一个软件(可能我的见识少)没有之一:如果你只停留在Excel处理数据,统计 ...

  5. 传说中的Markov"不过如此”

    因为看一篇题为 Passive Measurement of Interference in WiFi Network with Application in Misbehavior Detectio ...

  6. 【POJ】【1061】/【BZOJ】【1477】青蛙的约会

    扩展欧几里德 根据题意列出不定方程: (x+m*T)-(y+n*T)=k*L; //T表示跳了T次,由于是环,可能追了多圈,所以结果应为k*L 化简得  T(m-n)-kL=y-x; 这就成了我们熟悉 ...

  7. 剑指offer--面试题8

    题目:求旋转数组中的最小数字 以下为自己所写代码: #include "stdafx.h" #include <iostream> #include <excep ...

  8. 各种matrix

    http://www.gamedev.net/topic/602722-worldviewproj/

  9. 牛顿迭代法实现平方根函数sqrt

    转自利用牛顿迭代法自己写平方根函数sqrt 给定一个正数a,不用库函数求其平方根. 设其平方根为x,则有x2=a,即x2-a=0.设函数f(x)= x2-a,则可得图示红色的函数曲线.在曲线上任取一点 ...

  10. structs spring hibernate 三者之间有什么关系?

    现在开发流行MVC模式,structs在C(控制器)中使用:hibernate在M(模型)中被使用:至于 spring ,最大的作用在于,structs.hibernate的对象,由于在各个层之间相互 ...