【问题标题】:VB.Net Timer Pause/StopVB.Net 定时器暂停/停止
【发布时间】:2015-01-26 03:28:51
【问题描述】:

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

【问题讨论】:

    标签: vb.net timer countdowntimer


    【解决方案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 变量的代码,因此我认为它是一个对您的表单来说是全局的 datetime 变量。您可以像创建alarmTime 一样创建startTime 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 2023-04-09
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多