两个时间差的高精度检查

 1  static void Main(string[] args)
 2         {
 3             Console.WriteLine("开始时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff"));
 4             Stopwatch w = new Stopwatch();
 5             w.Start();
 6             Spin(w, 500);
 7             Console.ReadKey();
 8         }
 9         static void Spin(Stopwatch w, int duration)
10         {
11             var current = w.ElapsedMilliseconds;
12             while ((w.ElapsedMilliseconds - current) < duration)
13             {
14                 Thread.SpinWait(1);
15                 System.Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff"));
16             }
17             Console.WriteLine("最终时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff"));
18         }

运行结果如下

开始时间:

c#时间差高精度检查

结束时间:

c#时间差高精度检查

 

 刚刚好相差500毫秒

 

相关文章:

  • 2021-07-09
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2021-04-20
  • 2022-12-23
猜你喜欢
  • 2021-12-04
  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2021-12-12
  • 2022-01-23
相关资源
相似解决方案