WPF 计时器问题...无法获得正确的毫秒刻度
我有一个关于计时器的基本问题。我的计时器表现得很奇怪。我试图让滴答声每毫秒发生一次以更新我的数据。我似乎可以让它以秒为单位工作,但不能以毫秒为单位。
我正在使用 WPF,并且想知道为什么以下功能无法正常工作。
看起来“第二”倒计时工作正常,但是在使用相同的过程并编辑一个值时,它似乎没有正确“勾选”。
我正在尝试使用以下内容进行毫秒倒计时:
//TimeSpan temp0 = new TimeSpan(0, 0, 0, 0, 1);
CountdownTimer = new DispatcherTimer();
CountdownTimer.Tick += new EventHandler(Countdowntimer_Tick);
CountdownTimer.Interval = TimeSpan.FromSeconds(1.0);//temp0;
上面的内容似乎对于“秒”倒计时来说效果很好,但我需要更高的精度,所以我执行以下操作:
//TimeSpan temp0 = new TimeSpan(0, 0, 0, 0, 1);
IntroCountdownTimer = new DispatcherTimer();
IntroCountdownTimer.Tick += new EventHandler(Countdowntimer_Tick);
IntroCountdownTimer.Interval = TimeSpan.FromSeconds(0.001);//temp0;
这将为我们提供毫秒精度,但是,当我尝试时这在我的程序中要慢得多。有什么想法吗?
void Countdowntimer_Tick(object sender, EventArgs e)
{
m_dIntroCountdown -= 1.0;
}
ps:我确实相应地设置了“m_dIntroCountdown”。如果我们以毫秒为单位,我将其设置为 5000.0,如果以秒为单位,则将其设置为 5.0
也许我对此研究太多了..有什么想法吗?
感谢所有帮助。
谢谢!
I have a basic question regarding timers. My timer is acting very strange. I am trying to make the tick occur every millisecond to update my data. I can get it to work with seconds, it seems, but not milliseconds..
I am using WPF and am wondering why the following is not functioning correctly.
It appears that the "second" countdown works correctly, but while using the same procedure and editing one value, it does not "tick" correctly it seems.
I am trying to make a millisecond countdown using the following:
//TimeSpan temp0 = new TimeSpan(0, 0, 0, 0, 1);
CountdownTimer = new DispatcherTimer();
CountdownTimer.Tick += new EventHandler(Countdowntimer_Tick);
CountdownTimer.Interval = TimeSpan.FromSeconds(1.0);//temp0;
The above seems like it works fine for a "second" countdown, but I need more precision, so I do the following:
//TimeSpan temp0 = new TimeSpan(0, 0, 0, 0, 1);
IntroCountdownTimer = new DispatcherTimer();
IntroCountdownTimer.Tick += new EventHandler(Countdowntimer_Tick);
IntroCountdownTimer.Interval = TimeSpan.FromSeconds(0.001);//temp0;
This would give us millisecond precision, BUT, when I try this in my program, it is much much slower. Any ideas why?
void Countdowntimer_Tick(object sender, EventArgs e)
{
m_dIntroCountdown -= 1.0;
}
ps: I do set the "m_dIntroCountdown accordingly. If we are in milliseconds, I set it to 5000.0, if in seconds, 5.0
Maybe I am looking too much into this.. any ideas?
All help is appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要这个决议的目的是什么?如果您只是想跟踪时间,请使用 System.诊断。秒表。它的分辨率约为 10ns。
1 毫秒的时间分辨率对于 WPF 的处理能力来说太精细了。即使在 120 fps(较高)下,您也只能获得 8.3 毫秒的分辨率。为了以 1 毫秒更新,您需要每秒渲染 1000 帧。这超出了任何现代系统的限制。即使是人眼也开始在约 10 毫秒时失去对运动不连续变化的跟踪。
What do you want the resolution for? If you are just trying to keep track of time, use System.Diagnostics.Stopwatch. It has ~10ns resolution.
A 1 ms time resolution is way too fine for what WPF can handle. Even at 120 fps (which is high), you will only get 8.3 ms resolution. In order to update at 1ms, you'd need to render 1000 frames per second. This is just beyond the limits of any modern system. Even the human eye starts to lose track of discontinuous changes in motion at ~10ms.
这是 C# 的代码:
操作方法如下:
添加 3 个标签和 1 个按钮:
Label1
、Label2
、Label3
和Button1
这是 Vb(Visual Basic) 的代码:
This is the code for C#:
Here is how to do it:
Add 3 labels and 1 button :
Label1
,Label2
,Label3
andButton1
This is the code for Vb(Visual Basic):
DispatcherTimer 不是一个高精度计时器 - 它是一个低精度低准确度计时器,适合 UI 工作(人们不会注意到 100 毫秒的延迟)。
每 1 毫秒执行一次代码的高精度计时器是非常困难的,甚至可能是不可能实现的(如果系统中的其他进程达到 100% CPU 并且您的进程运行时间不超过 1 毫秒,您会怎么做?如果当时执行的代码必须从页面文件重新加载并且花费超过1ms怎么办?)。
DispatcherTimer is not an high precision timer - it's a low precision low accuracy timer suitable for UI work (where people don't notice delays of 100ms).
A high precision timers that execute code every 1ms is very difficult, maybe even impossible, to implement (what do you do if some other process in the system goes to 100% CPU and your process doesn't run for over 1ms? what do you do if the code executed by the time has to be reloaded from the page file and it takes more than 1ms?).