在.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控件

设计界面:

后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace TimerDemo
  11. {
  12. public partial class FrmMain : Form
  13. {
  14. //定义全局变量
  15. public int currentCount = ;
  16. public FrmMain()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private void FrmMain_Load(object sender, EventArgs e)
  22. {
  23. //设置Timer控件可用
  24. this.timer.Enabled = true;
  25. //设置时间间隔(毫秒为单位)
  26. this.timer.Interval = ;
  27. }
  28.  
  29. private void timer_Tick(object sender, EventArgs e)
  30. {
  31. currentCount += ;
  32. this.txt_Count.Text = currentCount.ToString().Trim();
  33. }
  34.  
  35. private void btn_Start_Click(object sender, EventArgs e)
  36. {
  37. //开始计时
  38. this.timer.Start();
  39. }
  40.  
  41. private void btn_Stop_Click(object sender, EventArgs e)
  42. {
  43. //停止计时
  44. this.timer.Stop();
  45. }
  46. }
  47. }

2、System.Timers.Timer

设计界面:

后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace TimersTimer
  11. {
  12. public partial class FrmMain : Form
  13. {
  14. //定义全局变量
  15. public int currentCount = ;
  16. //定义Timer类
  17. System.Timers.Timer timer;
  18. //定义委托
  19. public delegate void SetControlValue(string value);
  20.  
  21. public FrmMain()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. InitTimer();
  29. }
  30.  
  31. /// <summary>
  32. /// 初始化Timer控件
  33. /// </summary>
  34. private void InitTimer()
  35. {
  36. //设置定时间隔(毫秒为单位)
  37. int interval = ;
  38. timer = new System.Timers.Timer(interval);
  39. //设置执行一次(false)还是一直执行(true)
  40. timer.AutoReset = true;
  41. //设置是否执行System.Timers.Timer.Elapsed事件
  42. timer.Enabled = true;
  43. //绑定Elapsed事件
  44. timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
  45. }
  46.  
  47. /// <summary>
  48. /// Timer类执行定时到点事件
  49. /// </summary>
  50. /// <param name="sender"></param>
  51. /// <param name="e"></param>
  52. private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
  53. {
  54. try
  55. {
  56. currentCount += ;
  57. this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString());
  58. }
  59. catch (Exception ex)
  60. {
  61. MessageBox.Show("执行定时到点事件失败:" + ex.Message);
  62. }
  63. }
  64.  
  65. /// <summary>
  66. /// 设置文本框的值
  67. /// </summary>
  68. /// <param name="strValue"></param>
  69. private void SetTextBoxText(string strValue)
  70. {
  71. this.txt_Count.Text = this.currentCount.ToString().Trim();
  72. }
  73.  
  74. private void btn_Start_Click(object sender, EventArgs e)
  75. {
  76. timer.Start();
  77. }
  78.  
  79. private void btn_Stop_Click(object sender, EventArgs e)
  80. {
  81. timer.Stop();
  82. }
  83. }
  84. }

3、System.Threading.Timer

设计界面:

后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace Threading.Timer
  12. {
  13. public partial class FrmMain : Form
  14. {
  15. //定义全局变量
  16. public int currentCount = ;
  17. //定义Timer类
  18. System.Threading.Timer threadTimer;
  19. //定义委托
  20. public delegate void SetControlValue(object value);
  21.  
  22. public FrmMain()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void FrmMain_Load(object sender, EventArgs e)
  28. {
  29. InitTimer();
  30. }
  31.  
  32. /// <summary>
  33. /// 初始化Timer类
  34. /// </summary>
  35. private void InitTimer()
  36. {
  37. threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, );
  38. }
  39.  
  40. /// <summary>
  41. /// 定时到点执行的事件
  42. /// </summary>
  43. /// <param name="value"></param>
  44. private void TimerUp(object value)
  45. {
  46. currentCount += ;
  47. this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);
  48. }
  49.  
  50. /// <summary>
  51. /// 给文本框赋值
  52. /// </summary>
  53. /// <param name="value"></param>
  54. private void SetTextBoxValue(object value)
  55. {
  56. this.txt_Count.Text = value.ToString();
  57. }
  58.  
  59. /// <summary>
  60. /// 开始
  61. /// </summary>
  62. /// <param name="sender"></param>
  63. /// <param name="e"></param>
  64. private void btn_Start_Click(object sender, EventArgs e)
  65. {
  66. //立即开始计时,时间间隔1000毫秒
  67. threadTimer.Change(, );
  68. }
  69.  
  70. /// <summary>
  71. /// 停止
  72. /// </summary>
  73. /// <param name="sender"></param>
  74. /// <param name="e"></param>
  75. private void btn_Stop_Click(object sender, EventArgs e)
  76. {
  77. //停止计时
  78. threadTimer.Change(Timeout.Infinite, );
  79. }
  80. }
  81. }

代码下载链接:http://files.cnblogs.com/files/dotnet261010/Timer.rar

C#里面的三种定时计时器:Timer的更多相关文章

  1. C# winform三种定时方法

    1. 直接用winform 的 timers 拖控件进去 代码 public partial class Form1 : Form     {         public Form1()       ...

  2. 三种Timer使用

    System.Windows.Forms.Timer,  System.Threading.Timer,  System.Timer,三种Timer使用如下 第一种:System.Windows.Fo ...

  3. C# 计时器的三种使用方法

    在.net中有三种计时器,一是System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet;二是System.Timers命名空间下的Timer类. Timer控件 ...

  4. js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式

    js replace 全局替换   js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...

  5. 三种Timer

    一.基于 Windows 的标准计时器(System.Windows.Forms.Timer) 首先注意一点就是:Windows 计时器是为单线程环境设计的.它直接继承自Componet.Timer控 ...

  6. Python实现定时执行任务的三种方式简单示例

    本文实例讲述了Python实现定时执行任务的三种方式.分享给大家供大家参考,具体如下: 1.定时任务代码 import time,os,sched schedule = sched.scheduler ...

  7. 松软科技课堂:索引器计时器Timer

    在.NET中有三种计时器:1.System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet.Timer控件只有绑定了Tick事件和设置Enabled=True后才会 ...

  8. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  9. 【转】iOS学习之容易造成循环引用的三种场景

    ARC已经出来很久了,自动释放内存的确很方便,但是并非绝对安全绝对不会产生内存泄露.导致iOS对象无法按预期释放的一个无形杀手是——循环引用.循环引用可以简单理解为A引用了B,而B又引用了A,双方都同 ...

随机推荐

  1. 创建 StyledMapType 地图样式

    您可以通过创建 StyledMapType 并向构造函数传递特征和样式器信息,新建作为样式应用对象的地图类型.此方法不会影响默认地图类型的样式. 如需新建地图类型: 创建您的样式数组.请参阅“地图特征 ...

  2. RESTful WebService入门【转】

    ESTful WebService是比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状态的,发布和调用都非常的轻松容易.   下面写一 ...

  3. Ubuntu Pycharm不能同时选中多行解决方法

    转自http://blog.csdn.net/yaoqi_isee/article/details/77866309 问题描述 Pycharm和Sublime有一个很好用的特性就是可以同时选中多行进行 ...

  4. Unix环境高级编程(十四)守护进程实现时间服务器

    守护进程是在后台运行不受终端控制的进程(如输入.输出等),一般的网络服务都是以守护进程的方式运行.守护进程脱离终端的主要原因有两点:(1)用来启动守护进程的终端在启动守护进程之后,需要执行其他任务.( ...

  5. Factory - 工厂模式

    在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.但是在一些情况下, new操作符直接生成对象会带来一些问题.举例来说, 许多类型对象的创造需要一 ...

  6. Linux时间子系统(一) 基本概念

    本文使用Q & A的方式来和大家以前探讨一下时间的基本概念 一.什么是时间? 这个问题实在是太复杂了,我都不知道这是一个物理学.宇宙学.还是热力学异或是哲学问题,我只是想从几个侧面来了解一下时 ...

  7. Linux中网络通信中 使用的结构体

    "+++++++++++++++++++++++++ Linux TCP/UDP通信中的结构体 +++++++++++++++++++++++++++++++++++++++" s ...

  8. Dev BarManager使用方法

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking 近期使用BarManager时候.发现一个问题就是在一開始把BarManager控件拖到窗口上的时 ...

  9. tomcat6的编译和导入myeclipse

    声明:近期在学习tomcat6的源代码,网上搜索了些相关的资料,并自己操作了下进行了对应的汇总.如今总结例如以下 本文目的:编译tomcat6源代码+导入tomcat6源代码到myeclipse 測试 ...

  10. 解决maven构建webapp index.jsp报错问题

    今天早上想用maven 构建一个webapp 然后index.jsp华华丽丽的报错了  当时我的心情是一万头草泥马奔过啊,为啥你给我创建的webapp 还会报错啊!!!!!! 然后百度了一下,各种说少 ...