VB.Net 计时器暂停/停止



请任何人帮助我解决计时器上的问题。我将计时器设置为 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
计时器在

暂停时似乎一直运行的原因是您将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变量的代码,因此我认为它是一个日期时间变量,对您的表单是全局的。您可以像创建 alarmTime 一样创建 startTime 变量。

最新更新