2016-11-15 7 views
1

私はSystem.Windows.Forms.Timerを使用するアプリケーションを持っていて、タイマーの間隔に問題が見られています。 180秒の間に1000の間隔を使用すると、経過時間は常に約183秒です。 995の間隔を使用すると、経過時間は正確です(つまり180秒)。私は3つの異なるPCでこれを試してみましたが、それぞれ同じ結果が出ました。以下のコードは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分で2番目の差が十分にあるはずです。マルチメディアタイマーは、精度のためにできる最高のものです。ここに例がありますhttp://www.vbforums.com/showthread.php?837913-MicroTimer-Resource-Friendly-Accurate-Timer – topshot

+0

私はちょうど上記のコードを実行しています...他に何もありません。一貫性があり、間隔が1000で、一貫して正確であり、間隔は995です。 – JHH

答えて

-1

2つの容易に入手できるTimerオブジェクトの選択肢があります。

  • System.Threading.Timerは(私の最初のピック、hereを説明)
  • System.Timers.Timerhereを説明)
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 
関連する問題