WPF 计时器问题...无法获得正确的毫秒刻度

发布于 2024-09-08 10:26:04 字数 1133 浏览 2 评论 0原文

我有一个关于计时器的基本问题。我的计时器表现得很奇怪。我试图让滴答声每毫秒发生一次以更新我的数据。我似乎可以让它以秒为单位工作,但不能以毫秒为单位。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽梦忆笙歌 2024-09-15 10:26:04

你想要这个决议的目的是什么?如果您只是想跟踪时间,请使用 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.

被你宠の有点坏 2024-09-15 10:26:04

这是 C# 的代码:

using System.Windows.Threading;


public partial class MainWindow
{

    DateTime Time = new DateTime();
    DispatcherTimer timer1 = new DispatcherTimer();

    private void dispatchertimer_Tick(object sender, EventArgs e)
    {
        TimeSpan Difference = DateTime.Now.Subtract(Time);
        Label1.Content = Difference.Milliseconds.ToString();
        Label2.Content = Difference.Seconds.ToString();
        Label3.Content = Difference.Minutes.ToString();
    }

    private void Button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
        timer1.Tick += new System.EventHandler(dispatchertimer_Tick);
        timer1.Interval = new TimeSpan(0, 0, 0);


        if (timer1.IsEnabled == true)
        {
            timer1.Stop();
        }
        else
        {
            Time = DateTime.Now;
            timer1.Start();

        }
    }

操作方法如下:

添加 3 个标签和 1 个按钮:Label1Label2Label3Button1

这是 Vb(Visual Basic) 的代码:

Imports System.Windows.Threading

Class MainWindow

Dim timer1 As DispatcherTimer = New DispatcherTimer()
Dim Time As New DateTime

Private Sub dispatchertimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    Dim Difference As TimeSpan = DateTime.Now.Subtract(Time) 
    Label1.Content = Difference.Milliseconds.ToString
    Label2.Content = Difference.Seconds.ToString
    Label3.Content = Difference.Minutes.ToString
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    AddHandler timer1.Tick, AddressOf dispatchertimer_Tick
    timer1.Interval = New TimeSpan(0, 0, 0)


    If timer1.IsEnabled = True Then
        timer1.Stop()
    Else
        Time = DateTime.Now
        timer1.Start()

    End If
End Sub
End Class

This is the code for C#:

using System.Windows.Threading;


public partial class MainWindow
{

    DateTime Time = new DateTime();
    DispatcherTimer timer1 = new DispatcherTimer();

    private void dispatchertimer_Tick(object sender, EventArgs e)
    {
        TimeSpan Difference = DateTime.Now.Subtract(Time);
        Label1.Content = Difference.Milliseconds.ToString();
        Label2.Content = Difference.Seconds.ToString();
        Label3.Content = Difference.Minutes.ToString();
    }

    private void Button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
        timer1.Tick += new System.EventHandler(dispatchertimer_Tick);
        timer1.Interval = new TimeSpan(0, 0, 0);


        if (timer1.IsEnabled == true)
        {
            timer1.Stop();
        }
        else
        {
            Time = DateTime.Now;
            timer1.Start();

        }
    }

Here is how to do it:

Add 3 labels and 1 button : Label1, Label2, Label3 and Button1

This is the code for Vb(Visual Basic):

Imports System.Windows.Threading

Class MainWindow

Dim timer1 As DispatcherTimer = New DispatcherTimer()
Dim Time As New DateTime

Private Sub dispatchertimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    Dim Difference As TimeSpan = DateTime.Now.Subtract(Time) 
    Label1.Content = Difference.Milliseconds.ToString
    Label2.Content = Difference.Seconds.ToString
    Label3.Content = Difference.Minutes.ToString
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    AddHandler timer1.Tick, AddressOf dispatchertimer_Tick
    timer1.Interval = New TimeSpan(0, 0, 0)


    If timer1.IsEnabled = True Then
        timer1.Stop()
    Else
        Time = DateTime.Now
        timer1.Start()

    End If
End Sub
End Class
情仇皆在手 2024-09-15 10:26:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文