WPF 计时器问题...无法获得正确的毫秒刻度 [英] WPF Timer problem... Cannot get correct millisecond tick

查看:82
本文介绍了WPF 计时器问题...无法获得正确的毫秒刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于定时器的基本问题.我的计时器表现得很奇怪.我试图让滴答每毫秒发生一次以更新我的数据.我可以让它在几秒钟内工作,似乎,但不是毫秒..

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..

我正在使用 WPF 并且想知道为什么以下功能无法正常运行.

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:我确实相应地设置了m_dIntroCountdown.如果我们以毫秒为单位,我将其设置为 5000.0,如果以秒为单位,则为 5.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?

感谢所有帮助.

谢谢!

推荐答案

您希望分辨率用于什么?如果您只是想记录时间,请使用 系统.诊断.秒表.它的分辨率约为 10ns.

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.

1 毫秒的时间分辨率对于 WPF 可以处理的来说太过分了.即使在 120 fps(很高)下,您也只能获得 8.3 毫秒的分辨率.为了以 1 毫秒更新,您需要每秒渲染 1000 帧.这超出了任何现代系统的限制.甚至人眼也开始在大约 10 毫秒时开始忘记运动的不连续变化.

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.

这篇关于WPF 计时器问题...无法获得正确的毫秒刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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