一、主要属性、方法和事件

  Windows 窗体 Timer 是定期引发事件的组件。该组件是为 Windows 窗体环境设计的。

  时间间隔的长度由 Interval 属性定义,其值以毫秒为单位。若启用了该组件,则每个时间间隔引发一个 Tick 事件。

  这是添加要执行的代码的位置。

  Timer 组件的主要方法包括 Start 和Stop,这两种方法可打开和关闭计时器。计时器在关闭时重置;不存在暂停 Timer 组件的方法。

二、Windows 窗体 Timer 组件的 Interval 属性的限制

  Windows 窗体 Timer 组件具有一个 Interval 属性,该属性指定一个计时器事件与下一个计时器事件之间间隔的毫秒数。

  除非该组件被禁用,否则计时器会以大致相等的时间间隔继续接收 Tick 事件

  Interval 属性:当编写 Timer 组件时,需要考虑 Interval 属性的几点限制:

  • 如果应用程序或另一个应用程序对系统需求很大(如长循环、大量的计算或驱动程序、网络或端口访问),那么应用程序可能无法以 Interval 属性指定的频率来获取计时器事件。

  • 间隔可以在 1 和 64,767 之间(包括 1 和 64,767),这意味着即使最长的间隔(大约 64.8 秒)也不会超过一分钟很多。

  • 不能保证间隔所精确经过的时间。若要确保精确,计时器应根据需要检查系统时钟,而不是尝试在内部跟踪所积累的时间。

  • 系统每秒生成 18 个时钟刻度,因此即使 Interval 属性以毫秒为单位,间隔的实际精度也不会超过十八分之一秒。

  • System.Windows.Forms.Timer 类提供有关 Timer 类(用于 Windows 窗体计时器)及其成员的参考信息
三、使用 Windows 窗体计时器组件以设置的间隔运行过程
  有时您可能想创建一个过程,使其以特定时间间隔运行直至一个循环完成,或者使其在经过所设置的时间间隔后运行。Timer 组件使这种过程成为可能。
  1、在窗体中添加 Timer
  2、为计时器设置 Interval 属性(以毫秒为单位)。该属性决定在再次运行该过程之前所经过的时间。
    注意:计时器事件发生越频繁,用于响应该事件的处理器时间就越长。这会降低整体性能。请勿将间隔设置得比所需值小。
  3、在 Tick 事件处理程序内编写合适的代码。在该事件中编写的代码将以 Interval 属性中所指定的间隔运行。
  4、将 Enabled 属性设置为 true,以启动计时器。Tick 事件将开始发生,从而以设置的间隔运行过程。
  5、合适的时候,将 Enabled 属性设置为 false,以使过程停止再次运行。将间隔设置为 0,并不会导致计时器停止。
四、示例
  1、第一个代码示例以一秒为增量单位跟踪每天的时间。
  它在窗体中使用了 ButtonLabel 和 Timer 组件。将 Interval 属性设置为 1000(等于一秒钟)。
  在 Tick 事件中,将标签的标题设置为当前时间。
  当单击按钮时,Enabled 属性将设置为 false,以使计时器停止更新标签的标题。
  下面的代码示例要求您拥有一个窗体,该窗体具有一个名为 Button1 的 Button 控件、一个名为 Timer1 的 Timer 控件和一个名为 Label1 的 Label 控件。
  

 private void InitializeTimer()
{
//' Run this procedure in an appropriate event.
// Set to 1 second.
Timer1.Interval = ;
// Enable timer.
Timer1.Enabled = true;
Button1.Text = "Stop";
} private void Timer1_Tick(object Sender, EventArgs e)
{
// Set the caption to the current time.
Label1.Text = DateTime.Now.ToString();
} private void Button1_Click()
{
if ( Button1.Text == "Stop" )
{
Button1.Text = "Start";
Timer1.Enabled = false;
}
else
{
Button1.Text = "Stop";
Timer1.Enabled = true;
}
}

  2、第二个代码示例每隔 600 毫秒运行一次过程,直到循环完成时为止。

  下面的代码示例要求您拥有一个窗体,该窗体具有一个名为 Button1 的 Button 控件、一个名为 Timer1 的 Timer 控件和一个名为 Label1 的 Label 控件。

 // This variable will be the loop counter.
private int counter; private void InitializeTimer()
{
// Run this procedure in an appropriate event.
counter = ;
timer1.Interval = ;
timer1.Enabled = true;
// Hook up timer's tick event handler.
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
} private void timer1_Tick(object sender, System.EventArgs e)
{
if (counter >= )
{
// Exit loop code.
timer1.Enabled = false;
counter = ;
}
else
{
// Run your procedure here.
// Increment counter.
counter = counter + ;
label1.Text = "Procedures Run: " + counter.ToString();
}
}

详细信息查看:http://msdn.microsoft.com/zh-cn/library/beza71x7(v=vs.80).aspx

System.Windows.Forms.Timer的更多相关文章

  1. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  2. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  3. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  4. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

    System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...

  5. System.Windows.Forms.Timer反编译学习

    using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...

  6. System.Windows.Forms.Timer的简单用法

    Timer就是用来计时操作,如:你想在多少秒之后执行某个动作 Timer showTextBoxTimer = new Timer(); //新建一个Timer对象 showTextBoxTimer. ...

  7. System.Windows.Forms

    File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...

  8. .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll

    这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...

  9. WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

    WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...

随机推荐

  1. Telnet端口测试

    $IP ="220.181.111.142"$Port ="801" Function Port-Test ($IP,$Port){ $Timeout = 10 ...

  2. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  3. HDU 5235 Friends (2015 Multi-University Training Contest 2 搜索+剪枝)

    题目链接:pid=5305">传送门 题意: n个人给定m个关系.每一个关系为x,y表示x,y是朋友.可是可能是online friends,也可能是offline friends. ...

  4. Solaris 安装JDK

    http://blog.csdn.net/cymm_liu/article/details/46966237  整理自前辈的博客:http://segmentfault.com/a/119000000 ...

  5. linq小知识总结

    1linq的左连接查询 var boundList = from x in text.S_Outbound join y in text.S_Outbound_Per on x.Shipment_ID ...

  6. sqlserver 日期相关2

    1.常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233') (1)DATENAME ( datepart ,date ) 返回表示指定日期的指定日期部分的字符 ...

  7. java_类承继其他类的内部类例子

    package ming; class Outer { class In { public In(String msg) { System.out.println(msg); } } } public ...

  8. C语言第五节scanf函数

    变量的内存分析 字节和地址 为了更好地理解变量在内存中的存储细节,先来认识一下内存中的"字节"和"地址". 内存以"字节为单位" 0x表示的 ...

  9. CentOS/Linux安装VNCserver

    VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...

  10. 关于linux-Centos 7下mysql 5.7.9的rpm包的安装方式 (转)

    操作系统:Centos 7.1 mysql数据库版本:mysql5.7.9 mysql官方网站:http://www.mysql.com ------------------------------- ...