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

设计界面:

后台代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace TimerDemo
{
public partial class FrmMain : Form
{
//定义全局变量
public int currentCount = ;
public FrmMain()
{
InitializeComponent();
} private void FrmMain_Load(object sender, EventArgs e)
{
//设置Timer控件可用
this.timer.Enabled = true;
//设置时间间隔(毫秒为单位)
this.timer.Interval = ;
} private void timer_Tick(object sender, EventArgs e)
{
currentCount += ;
this.txt_Count.Text = currentCount.ToString().Trim();
} private void btn_Start_Click(object sender, EventArgs e)
{
//开始计时
this.timer.Start();
} private void btn_Stop_Click(object sender, EventArgs e)
{
//停止计时
this.timer.Stop();
}
}
}

2、System.Timers.Timer

设计界面:

后台代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace TimersTimer
{
public partial class FrmMain : Form
{
//定义全局变量
public int currentCount = ;
//定义Timer类
System.Timers.Timer timer;
//定义委托
public delegate void SetControlValue(string value); public FrmMain()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
InitTimer();
} /// <summary>
/// 初始化Timer控件
/// </summary>
private void InitTimer()
{
//设置定时间隔(毫秒为单位)
int interval = ;
timer = new System.Timers.Timer(interval);
//设置执行一次(false)还是一直执行(true)
timer.AutoReset = true;
//设置是否执行System.Timers.Timer.Elapsed事件
timer.Enabled = true;
//绑定Elapsed事件
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
} /// <summary>
/// Timer类执行定时到点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
currentCount += ;
this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString());
}
catch (Exception ex)
{
MessageBox.Show("执行定时到点事件失败:" + ex.Message);
}
} /// <summary>
/// 设置文本框的值
/// </summary>
/// <param name="strValue"></param>
private void SetTextBoxText(string strValue)
{
this.txt_Count.Text = this.currentCount.ToString().Trim();
} private void btn_Start_Click(object sender, EventArgs e)
{
timer.Start();
} private void btn_Stop_Click(object sender, EventArgs e)
{
timer.Stop();
}
}
}

3、System.Threading.Timer

设计界面:

后台代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading; namespace Threading.Timer
{
public partial class FrmMain : Form
{
//定义全局变量
public int currentCount = ;
//定义Timer类
System.Threading.Timer threadTimer;
//定义委托
public delegate void SetControlValue(object value); public FrmMain()
{
InitializeComponent();
} private void FrmMain_Load(object sender, EventArgs e)
{
InitTimer();
} /// <summary>
/// 初始化Timer类
/// </summary>
private void InitTimer()
{
threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, );
} /// <summary>
/// 定时到点执行的事件
/// </summary>
/// <param name="value"></param>
private void TimerUp(object value)
{
currentCount += ;
this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);
} /// <summary>
/// 给文本框赋值
/// </summary>
/// <param name="value"></param>
private void SetTextBoxValue(object value)
{
this.txt_Count.Text = value.ToString();
} /// <summary>
/// 开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Start_Click(object sender, EventArgs e)
{
//立即开始计时,时间间隔1000毫秒
threadTimer.Change(, );
} /// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Stop_Click(object sender, EventArgs e)
{
//停止计时
threadTimer.Change(Timeout.Infinite, );
}
}
}

代码下载链接: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. Google C++单元测试框架

    一.概述 Google C++单元测试框架(简称Gtest),可在多个平台上使用(包括Linux, Mac OS X, Windows, Cygwin和Symbian),它提供了丰富的断言.致命和非致 ...

  2. JS取date的前一天时间

    在javascript中取date的前一天时间: new Date(new Date()-24*60*60*1000),//取前一天的时间

  3. C# Timer使用方法示例

    实例化一个timer: // 每5分钟执行一次,每次执行的间隔毫秒时长 System.Timers.Timer timer = new System.Timers.Timer(5*60*1000); ...

  4. Open SSH原理

    OpenSSH(免费的 SSH 的实现)类似于 telnet 或rsh,ssh 客户程序也可以用于登录到远程机器.所要求的只是该远程机器正在运行 sshd,即 ssh 服务器进程.但是,与 telne ...

  5. slub分配器

    Linux的物理内存管理采用了以页为单位的buddy system(伙伴系统),但是很多情况下,内核仅仅需要一个较小的对象空间,而且这些小块的空间对于不同对象又是变化的.不可预测的,所以需要一种类似用 ...

  6. codeforces#254DIV2解题报告

    今天简直大爆发啊... 吃了顿烧烤竟然这么管事. . . .. 本弱渣竟然做出来了3道,并且B题是我第一次在CF中用到算法..(曾经最多也就是贪心. . . ). 题目地址:codeforces#22 ...

  7. failed to load selinux policy freezing

    一.原因: 在修改selinux配置文件时输入并保存了一个错误的配置参数. 二.挽救: 1. 重启系统. 2. 在启动选项上按 e. 3. 进入grub编辑页面. 4. 向下移动光标至fi下一行. 5 ...

  8. mysql-5.7 innodb change buffer 详解

    一.innodb change buffer 介绍: 1.innodb change buffer 是针对oltp场景下磁盘IO的一种优化(我也感觉这个不太像人话,但是它又非常的准确的说明 innod ...

  9. 强制删除一个Windows服务

    一个挂起的服务如下图所示,该服务相关的所有按钮都被禁用,包括启动.停止.暂停和恢复. 要停止这个服务,首先记住这个服务的名称,在这里是 ‘EntropySoftCFS’. 然后打开命令行窗口,运行 s ...

  10. C#修改GIF大小同时保持GIF仍然可动和背景透明

    /// <summary> /// 设置GIF大小 /// </summary> /// <param name="path">图片路径< ...