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

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

问题描述

运行以下(略伪)code产生以下结果。林震惊的计时器如何innacurate是(收益以14ms的〜每个勾选)。

有什么更精确的在那里?

 无效的主要()
{
   VAR定时器=新System.Threading.Timer(TimerCallback,NULL,0,1000);
}无效TimerCallback(对象状态)
{
   的Debug.WriteLine(DateTime.Now.ToString(ss.ffff));
}示例输出:
...
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


解决方案

有关确切的时间测量你需要使用秒表类
MSDN

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

Is there anything more accurate out there?

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

解决方案

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

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

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