2016-11-15 80 views
1

我有一个应用程序使用System.Windows.Forms.Timer,并看到定时器间隔的问题。当我在180秒的持续时间内使用1000的时间间隔时,持续时间一直在183秒左右。当我使用995的间隔时,经过的时间是准确的(即180秒)。我在3台不同的个人电脑上尝试过,每台电脑都有相同的结果。下面的代码使用2个文本框来显示开始和结束时间以及标签作为倒数计时器。Winforms定时器不准确

任何人都可以解释这种行为吗?

Public Class Form1 

Private WithEvents tmr As New System.Windows.Forms.Timer 

Private Duration As Integer = 180 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    RunTimer() 
End Sub 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    lblActivityTimer.Text = FormatCounter(Duration) 
    tmr.Interval = 1000 

End Sub 

Private Sub RunTimer() 

    tmr.Start() 
    TextBox1.Text = Now.ToString() 

End Sub 

Private Function FormatCounter(sec As Integer) As String 

    Dim Hours, Minutes, Seconds As Integer 

    Hours = sec \ 3600 
    Seconds = sec Mod 3600 
    Minutes = Seconds \ 60 
    Seconds = sec Mod 60 

    FormatCounter = Hours.ToString.PadLeft(2, "0"c) & ":" _ 
                & Minutes.ToString.PadLeft(2, "0"c) _ 
                & ":" _ 
                & Seconds.ToString.PadLeft(2, "0"c) 

End Function 

Private Sub tmr_Tick(sender As Object, e As EventArgs) Handles tmr.Tick 

    Duration -= 1 
    lblActivityTimer.Text = FormatCounter(Duration) 
    If Duration = 0 Then 
     TextBox2.Text = Now.ToString() 
     tmr.Stop() 
    End If 

End Sub 

End Class 
+1

这不是一个高精度计时器 - 蜱的活动实际上是在优先的排名非常低,这样他们可以在你期望 – Plutonix

+0

你必须做在你的应用程序公平一点是已经不远了,虽然好后发生。通常只有几毫秒的时间,所以应该在3分钟内完成第二次差异。多媒体计时器是您可以精确完成的最佳选择。这里是一个例子http://www.vbforums.com/showthread.php?837913-MicroTimer-Resource-Friendly-Accurate-Timer – topshot

+0

我只是运行上面的代码...没有别的。它始终不准确,间隔为1000,并且始终准确,间隔为995. – JHH

回答

-1

有两个现成的Timer对象备选方案:

  • System.Threading.Timer(解释here,我的第一选择)
  • System.Timers.Timer(解释here
0

使用Stop Watch,它提供了一组方法和属性,您可以使用这些方法和属性来精确测量已用时间。

Shared Sub Main(ByVal args() As String) 
    Dim stopWatch As New Stopwatch() 
    stopWatch.Start() 
    Thread.Sleep(10000) 
    stopWatch.Stop() 
    ' Get the elapsed time as a TimeSpan value. 
    Dim ts As TimeSpan = stopWatch.Elapsed 

    ' Format and display the TimeSpan value. 
    Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds/10) 
    Console.WriteLine("RunTime " + elapsedTime) 

End Sub 'Main