VB.Net 计时器 Pause/Stop
VB.Net Timer Pause/Stop
请任何人帮助我解决计时器上的问题。我将计时器设置为 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_Tick
和startButton_Click
事件需要更新。您还必须创建全局 startTime
变量。由于您没有显示有关如何创建 alarmTime
变量的代码,我假设它是一个日期时间变量,对您的表单是全局的。您可以像创建 alarmTime
.
一样创建 startTime
变量
请任何人帮助我解决计时器上的问题。我将计时器设置为 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_Tick
和startButton_Click
事件需要更新。您还必须创建全局 startTime
变量。由于您没有显示有关如何创建 alarmTime
变量的代码,我假设它是一个日期时间变量,对您的表单是全局的。您可以像创建 alarmTime
.
startTime
变量