.NET 中最准确的计时器? [英] Most accurate timer in .NET?

查看:20
本文介绍了.NET 中最准确的计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下(略微伪)代码会产生以下结果.我对计时器的不准确感到震惊(每个 Tick 增加约 14 毫秒).

Running the following (slightly pseudo)code produces the following results. Im shocked at how innacurate the timer is (gains ~14ms each Tick).

还有更准确的吗?

void Main()
{
   var timer = new System.Threading.Timer(TimerCallback, null, 0, 1000);
}

void TimerCallback(object state)
{
   Debug.WriteLine(DateTime.Now.ToString("ss.ffff"));
}

Sample Output:
...
11.9109
12.9190
13.9331
14.9491
15.9632
16.9752
17.9893
19.0043
20.0164
21.0305
22.0445
23.0586
24.0726
25.0867
26.1008
27.1148
28.1289
29.1429
30.1570
31.1710
32.1851

推荐答案

要精确测量时间,您需要使用 Stopwatch 类MSDN

For exact time measuring you need to use the Stopwatch class MSDN

这篇关于.NET 中最准确的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆