最近程式上用到了計時功能,對某個模塊進行計時,暫停的時候模塊也需要暫停,啟動的時候計時繼續
用到了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(); ////重新設定為零 //sw.Reset(); ////重新設定并開始計時 //sw.Restart(); ////結束計時 //sw.Stop(); //獲取運行時間間隔 TimeSpan ts = sw.Elapsed; //獲取運行時間[毫秒] long times = sw.ElapsedMilliseconds; //獲取運行的總時間 long times2 = sw.ElapsedTicks; //判斷計時是否正在進行[true為計時] bool isrun = sw.IsRunning; //獲取計時頻率 long frequency = Stopwatch.Frequency; //計時開始 sw.Start(); Thread.Sleep(1000); //計時結束 sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); //計時開始 sw.Start(); Thread.Sleep(2000); //計時結束 sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); sw.Start(); Thread.Sleep(3000); //計時結束 sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); } } }
需要進一步研究的同學可以查看官方檔案
Stopwatch 類 (System.Diagnostics) | Microsoft Docs
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434357.html
標籤:.NET技术
