Timer Pause Help

yogi_bear_79

Member
Joined
Sep 30, 2009
Messages
19
Programming Experience
Beginner
My timer pauses (sort of) the display portion (label) of the timer indeed pauses, however when the timer is resumed it starts again, but not at the point where it was disabled. As an example. If I pause the timer at 10:00 and wait ten seconds when I hit resume it starts at 09:50, where it should resume at 10:00 or 09:59

VB.NET:
Module Helper
    Public SessionTime As String
    Public Pause As Boolean = False
End Module



Public Class frmMain

Public SessionTimer As Date

Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Me.SessionTimer = Date.Now.AddMinutes(Val(SessionTime))
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim remainingTime As TimeSpan = Me.SessionTimer.Subtract(Date.Now)

        Me.lblTime.Text = String.Format("{0}:{1:d2}:{2:d2}", _
                                      remainingTime.Hours, _
                                    remainingTime.Minutes, _
                                  remainingTime.Seconds)

    End Sub

    Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click

        If Pause = False Then
            '...Button was labled "Pause" => Pause
            btnPause.Text = "Resume"
        Else
            '...Button was labled "Resume" => Resume
            btnPause.Text = "Pause"
        End If

        Timer1.Enabled = Pause '...Pause/Resume timer based on boolean state
        Pause = Not (Pause) '...Invert Boolean

    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.Timer1.Stop()
    End Sub
End Class
 
Here's a rearragement I'm sure you will find the logic in with time :D
VB.NET:
Private remainingTime As TimeSpan
Private timerTime As Date 'used to measure time between ticks

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim tmp As Date = Date.Now 'capture tick time
    remainingTime = remainingTime - (tmp - timerTime)
    timerTime = tmp 'reset tick time
    Me.lblTime.Text = String.Format("{0}:{1:d2}:{2:d2}", remainingTime.Hours, remainingTime.Minutes, remainingTime.Seconds)
End Sub
For Set button:
VB.NET:
remainingTime = New TimeSpan(0, 10, 0) '10 minutes
timerTime = Date.Now 'reset tick time
Timer1.Enabled = True
For Pause/Resume button:
VB.NET:
timerTime = Date.Now 'reset tick time
Timer1.Enabled = Not Timer1.Enabled 'toggle Timer
' also toggle button text
 
Another arrangement here with Stopwatch that perhaps is easier to read:
VB.NET:
Private timeset As TimeSpan
Private watch As Stopwatch

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim remainingTime As TimeSpan = timeset - watch.Elapsed
    Me.lblTime.Text = String.Format("{0}:{1:d2}:{2:d2}", remainingTime.Hours, remainingTime.Minutes, remainingTime.Seconds)
End Sub
For Set button:
VB.NET:
timeset = New TimeSpan(0, 10, 0)
watch = Stopwatch.StartNew
Timer1.Start()
For Pause/Resume button:
VB.NET:
If watch.IsRunning Then
    watch.Stop()
Else
    watch.Start()
End If
Timer1.Enabled = Not Timer1.Enabled
' also toggle button text
 
Back
Top