C#计时器,记录程序运行时长

System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); //  开始监视代码运行时间
//  需要测试的代码 ....
stopwatch.Stop(); //  停止监视
TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
double hours = timespan.TotalHours; // 总小时
double minutes = timespan.TotalMinutes;  // 总分钟
double seconds = timespan.TotalSeconds;  //  总秒数
double milliseconds = timespan.TotalMilliseconds;  //  总毫秒数
 

如果需要时间重置

stopwatch.Reset(); 

//引入命名空间
            using System.Diagnostics;
            //清空导入时间
            lbImportTime.Text = "";

            //开始记录时间

            Stopwatch myWatch = Stopwatch.StartNew();

            //记录运行时间

           //====================

           //停止记录时间

            myWatch.Stop();

           //显示运行时间

            lbImportTime.Text = myWatch.ElapsedMilliseconds.ToString();
 

猜你喜欢

转载自blog.csdn.net/zgscwxd/article/details/86562487