定时器间隔不一致

时间:2016-05-19 17:52:11

标签: vb.net timer

我有一个显示简单倒计时的System.Windows.Forms.Timer。间隔设置为1秒。执行时,执行定时器例程的延迟不一致,在1到12秒之间变化。计时器例程仅计算时间差并更新标签,以便它应在一秒钟内执行。此外,系统活动级别很低。有没有人知道为什么会这样?

以下是代码:

Public Class frmForceUserOffDialog
    Dim WithEvents tmrForceOff As System.Windows.Forms.Timer

    ' --------------
    ' Initiate timer
    ' --------------
    tmrForceOff = New System.Windows.Forms.Timer
    tmrForceOff.Interval = 1000
    tmrForceOff.Enabled = True
    tmrForceOff.Start()

    Private Sub tmrForceOff_tick() Handles tmrForceOff.Tick
        Dim strCountDown As String

        Console.WriteLine("tmrForceOff_tick: " & DateTime.Now.ToString)

        Try
            strCountDown = formatTimeDifference(DateTime.Now, dtmForceOffTime)
            ' ----------------------------------------
            ' Countdown reached, terminate application
            ' ----------------------------------------
            If strCountDown = "" Then
                Application.Exit()
            Else
                ' ---------------------
                ' Update countdown time
                ' ---------------------
                strCountDown = formatTimeDifference(DateTime.Now, dtmForceOffTime)
                Try
                    lblCountdown.Text = strCountDown
                    lblCountdown.Refresh()
                Catch ex As Exception
                End Try
            End If
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

0 个答案:

没有答案