網站首頁 編程語言 正文
在.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 = 0; public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { //設置Timer控件可用 this.timer.Enabled = true; //設置時間間隔(毫秒為單位) 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) { //開始計時 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 = 0; //定義Timer類 System.Timers.Timer timer; //定義委托 public delegate void SetControlValue(string value); public FrmMain() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { InitTimer(); } ////// 初始化Timer控件 /// private void InitTimer() { //設置定時間隔(毫秒為單位) int interval = 1000; 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); } ////// Timer類執行定時到點事件 /// /// /// private void TimerUp(object sender, System.Timers.ElapsedEventArgs e) { try { currentCount += 1; this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString()); } catch (Exception ex) { MessageBox.Show("執行定時到點事件失敗:" + ex.Message); } } ////// 設置文本框的值 /// /// 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 = 0; //定義Timer類 System.Threading.Timer threadTimer; //定義委托 public delegate void SetControlValue(object value); public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { InitTimer(); } ////// 初始化Timer類 /// private void InitTimer() { threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000); } ////// 定時到點執行的事件 /// /// private void TimerUp(object value) { currentCount += 1; this.Invoke(new SetControlValue(SetTextBoxValue), currentCount); } ////// 給文本框賦值 /// /// private void SetTextBoxValue(object value) { this.txt_Count.Text = value.ToString(); } ////// 開始 /// /// /// private void btn_Start_Click(object sender, EventArgs e) { //立即開始計時,時間間隔1000毫秒 threadTimer.Change(0, 1000); } ////// 停止 /// /// /// private void btn_Stop_Click(object sender, EventArgs e) { //停止計時 threadTimer.Change(Timeout.Infinite, 1000); } } }
代碼下載鏈接:點此下載
原文鏈接:https://www.cnblogs.com/dotnet261010/p/7113523.html
相關推薦
- 2022-05-17 C++設計模式狀態模式(State)
- 2023-07-07 Linux服務器zip安裝,及壓縮解壓
- 2022-04-14 zsh: command not found:快速的解決方法
- 2022-04-01 docker 命令查看registry倉庫鏡像
- 2022-07-09 嵌入式linux使用trace調試步驟記錄
- 2022-12-26 一文帶你熟悉Go語言中函數的使用_Golang
- 2022-09-05 Spark/Hive 行列轉換
- 2022-10-02 Selenium+Python自動化測試入門_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支