Reputation: 119
please anyone help me on my problem on timer. I set my timer to 1minute. (60seconds). By clicking the start and pause button runs well but after click another button to resume the time it does not accurate on what time i pause. Example: I start my timer (1minute) and pause to 40seconds. After i resume, the time is not exactly on what my time pause. Instead of 40 seconds it starts in 30 like that depends on what time i click the resume button. It just like it continues in running even i stop the timer. Here is my code.
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
Upvotes: 0
Views: 5901
Reputation: 1372
The reason your timer seems to keep running while it is paused is because you are comparing the alartTime
to the computer system time. Obviously the system time on a computer keeps changing every second and doesn't obey pauses. When you resume your timer it is still comparing to the current time which has inevitably changed since the pause.
To solve this issue I would store a copy of the current time when you press the start button and compare the alarm time to the saved start time which will no longer change:
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
Note: Only the Timer2_Tick
and startButton_Click
events need to be updated. You must also create the global startTime
variable as well. Since you didn't show the code for how you created the alarmTime
variable, I assumed it was a datetime variable that was global to your form. You can create the startTime
variable the same way you created alarmTime
.
Upvotes: 1