VB.Net定时器暂停/停止

时间:2015-01-26 03:28:52

标签: vb.net timer countdowntimer

请有人帮我解决我的计时器问题。我把我的计时器设置为1分钟。 (60秒)。通过单击开始和暂停按钮运行良好但单击另一个按钮以恢复时间它不准确我暂停的时间。 示例:我启动计时器(1分钟)并暂停到40秒。我恢复之后,时间并不完全取决于我的暂停时间。而不是40秒,它开始在30,这取决于我点击恢复按钮的时间。它就像它继续运行甚至我停止计时器。这是我的代码。

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

    If alarmTime < Date.Now Then
        ' Display the new time left 
        ' by updating the Time Left label.
        Timer2.Stop()
        MessageBox.Show("Times Up!.", "Thank you!")
        BtnBack.Enabled = True
        startButton.Enabled = False
        BtnSubmit.Enabled = False
        AnsA.Enabled = False
        AnsB.Enabled = False
        AnsC.Enabled = False
        AnsD.Enabled = False
        BtnNext.Enabled = False
        BtnPrev.Enabled = False
        BtnClose.Enabled = True
        Categoriess.lnkMathHS.Enabled = False
    Else
        Dim remainingtime As TimeSpan '= Me.alarmTime.Subtract(Date.Now)
        remainingtime = Me.alarmTime.Subtract(Date.Now)
        timeLabel.Text = String.Format("{0}:{1:d2}:{2:d2}", _
        remainingtime.Hours, _
        remainingtime.Minutes, _
        remainingtime.Seconds)
    End If

End Sub

Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click

    alarmTime = Date.Now.AddMinutes(TextBox1.Text)
    Timer2.Start()

End Sub


Private Sub resumeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resumeButton.Click

    Timer2.start()

End Sub

 Private Sub stopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click

    Timer2.stop()

End Sub

1 个答案:

答案 0 :(得分:1)

您的计时器在暂停时似乎继续运行的原因是您将alartTime与计算机系统时间进行比较。显然,计算机上的系统时间每秒都在不断变化,并且不服从暂停。当你恢复计时器时,它仍然会与暂停后不可避免地发生变化的当前时间进行比较。

要解决此问题,我会在您按下开始按钮时将当前时间的副本存储起来,并将闹钟时间与保存的开始时间进行比较,后者将不再更改:

Dim alarmTime As DateTime
Dim startTime As DateTime   ' New start time variable to save a copy of the current date/time when the start button is clicked

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    Dim remainingtime As TimeSpan

    alarmTime = alarmTime.AddSeconds(-1)    ' subtract 1 second from the alarm time
    remainingtime = Me.alarmTime.Subtract(startTime)    ' get the amount of time between the saved start time and the current alarm time

    If alarmTime >= startTime Then
        ' There is still more time left on the alarm so we update the label with the subtracted time

        timeLabel.Text = String.Format("{0}:{1:d2}:{2:d2}", _
        remainingtime.Hours, _
        remainingtime.Minutes, _
        remainingtime.Seconds)
    End If

    If remainingtime.TotalSeconds = 0 Then
        ' The time has elapsed

        ' Display the new time left 
        ' by updating the Time Left label.
        Timer2.Stop()
        MessageBox.Show("Times Up!.", "Thank you!")
        BtnBack.Enabled = True
        startButton.Enabled = False
        BtnSubmit.Enabled = False
        AnsA.Enabled = False
        AnsB.Enabled = False
        AnsC.Enabled = False
        AnsD.Enabled = False
        BtnNext.Enabled = False
        BtnPrev.Enabled = False
        BtnClose.Enabled = True
        Categoriess.lnkMathHS.Enabled = False
    End If
End Sub

Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click

    Me.Timer2.Interval = 1000   ' Ensure that the timer is ticking once per second
    startTime = Date.Now    ' Save a copy of the current date/time
    alarmTime = Date.Now.AddMinutes(TextBox1.Text)
    Timer2.Start()

End Sub

注意:只需要更新Timer2_TickstartButton_Click个事件。您还必须创建全局startTime变量。由于您没有显示有关如何创建alarmTime变量的代码,因此我假设它是一个对您的表单而言是全局的日期时间变量。您可以使用与创建startTime相同的方式创建alarmTime变量。