日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(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

欄目分類(lèi)
最近更新