2010-07-05 74 views
1

我有一个关于计时器的基本问题。我的计时器非常奇怪。我正在尝试使每个毫秒发生一次更新以更新我的数据。我可以得到它与秒,但似乎,但不是毫秒的工作..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

也许我期待太多到这个..任何想法。 ?

所有帮助表示赞赏。

谢谢!

回答

5

1 ms的时间分辨率的方式,对于什么WPF可以处理得很好。即使在120帧(这是高),你会只有8.3毫秒的分辨率。为了以1ms更新,您需要渲染每秒1000帧。这超出了现代系统的限制。即使人眼也开始失去~10ms运动的不连续变化。

你想要什么决议?如果您只是想跟踪时间,请使用System.Diagnostics.Stopwatch。它具有〜10ns的分辨率。

+0

Noooo,我是t试图更新每秒1000帧的WPF :) 我想提供一个高精度的计时器。 至于秒表信息,真棒。谢谢。 – Kyle 2010-07-05 23:07:29

+1

@凯尔:你可以接受答案。 – Amsakanna 2010-07-06 08:23:28

+1

接受,谢谢! (这里还是新的) – Kyle 2010-07-07 03:29:34

0

DispatcherTimer不是一个高精度的定时器 - 它是一个适用于UI工作的低精度低精度定时器(人们不会注意到100ms的延迟)。

每1ms执行一次代码的高精度定时器很难甚至不可能实现(如果系统中的某些其他进程达到100%CPU并且进程不能运行超过1ms ?如果由时间执行的代码必须从页面文件重新加载并且需要超过1毫秒,那么您会做什么?)。

2

这是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