網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言:
在.NET中,已經(jīng)存在了5個(gè)Timer類:
System.Threading.Timer System.Timers.Timer System.Web.UI.Timer System.Windows.Forms.Timer System.Windows.Threading.DispatcherTimer
不管以前這樣設(shè)計(jì)的原因,現(xiàn)在.NET 6又為我們?cè)黾恿艘粋€(gè)新Timer
,??PeriodicTimer
??。
這又是為什么呢?
一、Demo
與其他Timer需要?jiǎng)?chuàng)建事件回調(diào)不同:
Timer timer = new Timer(delegate { ? ? Thread.Sleep(3000); ? ? Console.WriteLine($"Timer Thread: {Thread.CurrentThread.ManagedThreadId}"); ? ? Console.WriteLine($"{DateTime.Now.Second} Timer tick"); },null,0,1000 );
PeriodicTimer的使用方式如下:
//間隔時(shí)間1秒 using (var timer = new PeriodicTimer(TimeSpan.FromSeconds(1))) { ? ? //在到達(dá)指定周期后執(zhí)行方法 ? ? while (await timer.WaitForNextTickAsync()) ? ? { ? ? ? ? await Task.Delay(3000); ? ? ? ? ? Console.WriteLine($"Timer Thread: {Thread.CurrentThread.ManagedThreadId}"); ? ? ? ? Console.WriteLine($"{DateTime.Now.Second} PeriodicTimer tick"); ? ? } }
?從??await
??關(guān)鍵字可以看出,PeriodicTimer
用于異步執(zhí)行;并且一次只有一個(gè)線程可以執(zhí)行。
另外,你可以控制?停止PeriodicTimer計(jì)時(shí)?。示例代碼如下:
//創(chuàng)建CancellationTokenSource,指定在3秒后將被取消 var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3)); using (var timer = new PeriodicTimer(TimeSpan.FromSeconds(1))) { ? ? while (await timer.WaitForNextTickAsync(cts.Token)) ? ? { ? ? ? ? Console.WriteLine($"{DateTime.Now.Second} PeriodicTimer tick"); ? ? } }
需要注意的是,這會(huì)引發(fā)??OperationCancelled??異常,你需要捕獲該異常,然后根據(jù)需要進(jìn)行處理:
當(dāng)然,你也可以通過(guò)主動(dòng)取消CancellationTokenSource
,來(lái)停止PeriodicTimer
計(jì)時(shí),
示例代碼如下:
var cts = new CancellationTokenSource(); using (var timer = new PeriodicTimer(TimeSpan.FromSeconds(1))) { ? ? int count = 0; ? ? while (await timer.WaitForNextTickAsync(cts.Token)) ? ? { ? ? ? ? if (++count == 3) ? ? ? ? { ? ? ? ? ? ? //執(zhí)行3次后取消 ? ? ? ? ? ? cts.Cancel(); ? ? ? ? } ? ? ? ? Console.WriteLine($"{DateTime.Now.Second} PeriodicTimer tick"); ? ? } }
這次換成了??TaskCancelled??異常:
如果,你不想拋出異常,則可以用PeriodicTimer.Dispose
方法來(lái)停止計(jì)時(shí),
示例代碼如下:
using (var timer = new PeriodicTimer(TimeSpan.FromSeconds(1))) { ? ? int count = 0; ? ? while (await timer.WaitForNextTickAsync()) ? ? { ? ? ? ? if (++count == 3) ? ? ? ? { ? ? ? ? ? ? //執(zhí)行3次后取消 ? ? ? ? ? ? timer.Dispose(); ? ? ? ? } ? ? ? ? Console.WriteLine($"{DateTime.Now.Second} PeriodicTimer tick"); ? ? } }
結(jié)論:
通過(guò)上面的代碼,可以了解到,設(shè)計(jì)PeriodicTimer的原因,可以歸結(jié)為:
- 用于異步上下文
- 一次僅由一個(gè)消費(fèi)者使用?
原文鏈接:https://blog.51cto.com/MyIO/5026597
相關(guān)推薦
- 2022-07-19 react組件通訊的基本使用props
- 2022-11-13 Git如何恢復(fù)到之前版本_相關(guān)技巧
- 2023-01-18 C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù)_C#教程
- 2022-07-04 Python異步處理返回進(jìn)度——使用Flask實(shí)現(xiàn)進(jìn)度條_python
- 2023-01-13 Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決_python
- 2022-12-21 淺析Go語(yǔ)言的數(shù)據(jù)類型及數(shù)組_Golang
- 2022-12-04 python中的list字符串元素排序_python
- 2022-10-16 Qt實(shí)現(xiàn)進(jìn)程間通信_(tái)C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支