.NET System.Diagnostics.Stopwatch issue (returns values too low)

前端 未结 7 1101
眼角桃花
眼角桃花 2021-02-13 23:14

On my computer the Stopwatch is returning values way too low. For example, 200 ms when I specified Thread.Sleep(1000). The program is supposed to wait 1 second. I a

相关标签:
7条回答
  • 2021-02-14 00:16

    You can use that code to fix "Stopwatch.Elapsed" method problem:

    using System;
    using System.Diagnostics;
    
    namespace HQ.Util.General
    {
        public static class StopWatchExtension
        {
            public static TimeSpan ToTimeSpan(this Stopwatch stopWatch)
            {
                return TimeSpan.FromTicks(stopWatch.ElapsedTicks);
            }
        }
    }
    

    Usage:

    using HQ.Util.General;
    
    Debug.Print($"Elapsed millisec: { stopWatch.ToTimeSpan().TotalMilliseconds}");
    
    0 讨论(0)
提交回复
热议问题