網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
C#?中使用Stopwatch計(jì)時(shí)器實(shí)現(xiàn)暫停計(jì)時(shí)繼續(xù)計(jì)時(shí)功能_C#教程
作者:小嘛小兒郎 ? 更新時(shí)間: 2022-05-11 編程語(yǔ)言最近程序上用到了計(jì)時(shí)功能,對(duì)某個(gè)模塊進(jìn)行計(jì)時(shí),暫停的時(shí)候模塊也需要暫停,啟動(dòng)的時(shí)候計(jì)時(shí)繼續(xù)
用到了Stopwatch
Stopwatch的命名空間是using System.Diagnostics;
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp3 { class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); ////重新設(shè)置為零 //sw.Reset(); ////重新設(shè)置并開(kāi)始計(jì)時(shí) //sw.Restart(); ////結(jié)束計(jì)時(shí) //sw.Stop(); //獲取運(yùn)行時(shí)間間隔 TimeSpan ts = sw.Elapsed; //獲取運(yùn)行時(shí)間[毫秒] long times = sw.ElapsedMilliseconds; //獲取運(yùn)行的總時(shí)間 long times2 = sw.ElapsedTicks; //判斷計(jì)時(shí)是否正在進(jìn)行[true為計(jì)時(shí)] bool isrun = sw.IsRunning; //獲取計(jì)時(shí)頻率 long frequency = Stopwatch.Frequency; //計(jì)時(shí)開(kāi)始 sw.Start(); Thread.Sleep(1000); //計(jì)時(shí)結(jié)束 sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); Thread.Sleep(2000); Thread.Sleep(3000); } } }
需要進(jìn)一步研究的同學(xué)可以查看官方文檔
Stopwatch 類(lèi) (System.Diagnostics) | Microsoft Docs
原文鏈接:https://www.cnblogs.com/xing2/p/15945256.html
相關(guān)推薦
- 2024-03-14 AOP切面編程,以及自定義注解實(shí)現(xiàn)切面
- 2023-07-28 Cannot read properties of undefined (reading ‘push
- 2022-05-24 Golang?錯(cuò)誤捕獲Panic與Recover的使用_Golang
- 2022-08-07 Android?Gradle?插件自定義Plugin實(shí)現(xiàn)注意事項(xiàng)_Android
- 2022-05-22 C#單例模式與多線程用法介紹_C#教程
- 2023-07-24 vxe-grid實(shí)現(xiàn) 二維數(shù)據(jù)聯(lián)動(dòng)
- 2023-03-28 react-redux及redux狀態(tài)管理工具使用詳解_React
- 2024-04-06 @Delete通過(guò)批量刪除的方法
- 最近更新
-
- 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概述快速入門(mén)
- 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)程分支