C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹
在.NET中有三種計(jì)時(shí)器:
- 1、System.Windows.Forms命名空間下的Timer控件,它直接繼承自Componet。Timer控件只有綁定了Tick事件和設(shè)置Enabled=True后才會(huì)自動(dòng)計(jì)時(shí),停止計(jì)時(shí)可以用Stop()方法控制,通過Stop()停止之后,如果想重新計(jì)時(shí),可以用Start()方法來啟動(dòng)計(jì)時(shí)器。Timer控件和它所在的Form屬于同一個(gè)線程;
- 2、System.Timers命名空間下的Timer類。System.Timers.Timer類:定義一個(gè)System.Timers.Timer對(duì)象,然后綁定Elapsed事件,通過Start()方法來啟動(dòng)計(jì)時(shí),通過Stop()方法或者Enable=false停止計(jì)時(shí)。AutoReset屬性設(shè)置是否重復(fù)計(jì)時(shí)(設(shè)置為false只執(zhí)行一次,設(shè)置為true可以多次執(zhí)行)。Elapsed事件綁定相當(dāng)于另開了一個(gè)線程,也就是說在Elapsed綁定的事件里不能訪問其它線程里的控件(需要定義委托,通過Invoke調(diào)用委托訪問其它線程里面的控件)。
- 3、System.Threading.Timer類。定義該類時(shí),通過構(gòu)造函數(shù)進(jìn)行初始化。
在上面所述的三種計(jì)時(shí)器中,第一種計(jì)時(shí)器和它所在的Form處于同一個(gè)線程,因此執(zhí)行的效率不高;而第二種和第三種計(jì)時(shí)器執(zhí)行的方法都是新開一個(gè)線程,所以執(zhí)行效率比第一種計(jì)時(shí)器要好,因此在選擇計(jì)時(shí)器時(shí),建議使用第二種和第三種。
下面是三種定時(shí)器使用的例子:
1、Timer控件
設(shè)計(jì)界面:

后臺(tái)代碼:
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 = 0;
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
//設(shè)置Timer控件可用
this.timer.Enabled = true;
//設(shè)置時(shí)間間隔(毫秒為單位)
this.timer.Interval = 1000;
}
private void timer_Tick(object sender, EventArgs e)
{
currentCount += 1;
this.txt_Count.Text = currentCount.ToString().Trim();
}
private void btn_Start_Click(object sender, EventArgs e)
{
//開始計(jì)時(shí)
this.timer.Start();
}
private void btn_Stop_Click(object sender, EventArgs e)
{
//停止計(jì)時(shí)
this.timer.Stop();
}
}
}2、System.Timers.Timer
設(shè)計(jì)界面:

后臺(tái)代碼:
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 = 0;
//定義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()
{
//設(shè)置定時(shí)間隔(毫秒為單位)
int interval = 1000;
timer = new System.Timers.Timer(interval);
//設(shè)置執(zhí)行一次(false)還是一直執(zhí)行(true)
timer.AutoReset = true;
//設(shè)置是否執(zhí)行System.Timers.Timer.Elapsed事件
timer.Enabled = true;
//綁定Elapsed事件
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
}
/// <summary>
/// Timer類執(zhí)行定時(shí)到點(diǎn)事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
currentCount += 1;
this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString());
}
catch (Exception ex)
{
MessageBox.Show("執(zhí)行定時(shí)到點(diǎn)事件失敗:" + ex.Message);
}
}
/// <summary>
/// 設(shè)置文本框的值
/// </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
設(shè)計(jì)界面:

后臺(tái)代碼:
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 = 0;
//定義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, 1000);
}
/// <summary>
/// 定時(shí)到點(diǎn)執(zhí)行的事件
/// </summary>
/// <param name="value"></param>
private void TimerUp(object value)
{
currentCount += 1;
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)
{
//立即開始計(jì)時(shí),時(shí)間間隔1000毫秒
threadTimer.Change(0, 1000);
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Stop_Click(object sender, EventArgs e)
{
//停止計(jì)時(shí)
threadTimer.Change(Timeout.Infinite, 1000);
}
}
}代碼下載鏈接:點(diǎn)此下載
到此這篇關(guān)于C#中的三種定時(shí)計(jì)時(shí)器Timer用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#中高精度計(jì)時(shí)器Stopwatch的用法詳解
- C#使用Stopwatch實(shí)現(xiàn)計(jì)時(shí)功能
- C#?Stopwatch實(shí)現(xiàn)計(jì)算代碼運(yùn)行時(shí)間
- C#中Stopwatch的使用及說明
- C# 中使用Stopwatch計(jì)時(shí)器實(shí)現(xiàn)暫停計(jì)時(shí)繼續(xù)計(jì)時(shí)功能
- 如何使用C# Stopwatch 測(cè)量微秒級(jí)精確度
- .NET/C# 使用Stopwatch測(cè)量運(yùn)行時(shí)間
- C#使用StopWatch獲取程序毫秒級(jí)執(zhí)行時(shí)間的方法
- C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析
- C#中的Timer和DispatcherTimer使用實(shí)例
- C#中三種Timer計(jì)時(shí)器的詳細(xì)用法
- 詳解C#中的定時(shí)器Timer類及其垃圾回收機(jī)制
- C#使用timer實(shí)現(xiàn)的簡(jiǎn)單鬧鐘程序
- [C#].NET中幾種Timer的使用實(shí)例
- C# 中Stopwatch和timer的實(shí)現(xiàn)示例
相關(guān)文章
C#版的 Escape() 和 Unescape() 函數(shù)分享
從網(wǎng)上看到兩個(gè)方法, C# 版的 Escape() 和 Unescape(),收藏下。2011-05-05
C#實(shí)現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實(shí)例,本文思路同樣適用必應(yīng)、搜狗、搜搜、360等搜索引擎,需要的朋友可以參考下2015-01-01
C#使用Free?Spire.PDF進(jìn)行PDF打印的實(shí)現(xiàn)方案
在現(xiàn)代應(yīng)用開發(fā)中,打印?PDF?文件是一個(gè)常見需求,C#?提供了多種庫(kù)來支持這一功能,其中?Free?Spire.PDF?for?.NET?是一個(gè)不錯(cuò)的選擇,本文將深入解析如何使用?Free?Spire.PDF?進(jìn)行?PDF?打印,需要的朋友可以參考下2025-08-08
C#中實(shí)現(xiàn)判斷某個(gè)類是否實(shí)現(xiàn)了某個(gè)接口
這篇文章主要介紹了C#中實(shí)現(xiàn)判斷某個(gè)類是否實(shí)現(xiàn)了某個(gè)接口,本文給出了多種判斷方法,需要的朋友可以參考下2015-06-06
PowerShell 定時(shí)執(zhí)行.Net(C#)程序的方法
利用PowerShell可以調(diào)用動(dòng)態(tài)頁面,然后再用 .bat 執(zhí)行 PowerShell 腳本,最后把 .bat 添加到服務(wù)器的任務(wù)計(jì)劃里面。OK,所有操作都做好了,.Net 定時(shí)執(zhí)行了,是不是呢,有木有呢。2013-04-04
C#實(shí)現(xiàn)的pdf生成圖片文字水印類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的pdf生成圖片文字水印類,結(jié)合完整實(shí)例形式分析了C#針對(duì)pdf文件的創(chuàng)建、添加文字、水印等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
C# 委托中 Invoke/BeginInvoke/EndInvoke和DynamicInvoke&
在C#中,委托(Delegate)提供了多種調(diào)用方式,包括 Invoke、BeginInvoke、EndInvoke 和 DynamicInvoke,每種調(diào)用方式都有其特定的用途和適用場(chǎng)景,下面將詳細(xì)介紹這些方法的區(qū)別與聯(lián)系,感興趣的朋友一起看看吧2025-03-03

