时间:2023-03-19 11:58:47 | 栏目:.NET代码 | 点击:次
最近程序上用到了计时功能,对某个模块进行计时,暂停的时候模块也需要暂停,启动的时候计时继续
用到了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(); Thread.Sleep(2000); Thread.Sleep(3000); } } }
需要进一步研究的同学可以查看官方文档