Stop and restart timer ??

KNN

Member
Joined
Oct 6, 2006
Messages
14
Programming Experience
1-3
I'm trying to stop and than start my timer again at the point that I stopped it. I'm able to stop it, but when I click to continute counting down, it does continue but not where I stoped it, continues from the point where it would be as if I never stopped it. I guess it runs in the background. It shouldn't these 3 subs show all I do with the timer.

Private
Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.alarmTime = Date.Now.AddMinutes(15.02)
Me.Timer1.Enabled = True
Me.Timer1.Start()
End Sub


Private Sub StopRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "Stop Timer" Then
Me.Timer1.Stop()
Button2.Text = "Restart Timer"
ElseIf Button2.Text = "Restart Timer" Then
Me.Timer1.Start()
Button2.Text =
"Stop Timer"
End If

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If alarmTime < Date.Now Then
Me.Timer1.Stop()
MessageBox.Show(
"End of the quarter")
Else
Dim remainingTime As TimeSpan = Me.alarmTime.Subtract(Date.Now)
Me.TextBox15.Text = String.Format("{0:d2}:{1:d2}", remainingTime.Minutes, remainingTime.Seconds)
End If
End Sub
 
When you stop the timer you only stop the tick event. So, when the timer is restarted, your alarmtime is still the same number, and then you check the remaining time as "Now" so the amount of time between alarmtime and now isn't going to change.

If I'm not misunderstanding, you want the timer to stop and the amount of time that it is stopped for to basically be added to the remaining time, right?
 
I think then you just have a problem with the logic. Rather than setting alarmtime to an actualy time, you need to set it to a number of minutes or hours (stored as seconds).

Then with a timer (having an interval of 1000ms) subtract one second from the number of seconds remaining. When that hits 0, you sound the alarm.

This way, all you have to do to stop the countdown is to put a Timer1.Enabled=False in a button event, and in another button Timer1.Enable=True to restart it.


Your current logic would work more like a "sound the alarm AT [ThisTime] unless the event is disabled". I think what you want is the "sound the alarm when the timer hits 0" as above.
 
"Then with a timer (having an interval of 1000ms) subtract one second from the number of seconds remaining. When that hits 0, you sound the alarm."

can you write me some code for this, I can't understand how to do it.

thanks
 
Add a timer, a label, and two buttons to your form, then paste this code below the "Windows Form Designer generated code" region.

This will set button one as start, button two as stop. The label will be refreshed with the seconds remaining each tick of the timer. The timer will stop as desired, at the seconds remaining, and resume when start (button1) is clicked.


VB.NET:
Dim intSecondsUntilAlarm As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myTime As Date
        Dim myAlarmTime As Date

        myTime = Now
        myAlarmTime = Now.AddHours(2) 'Number of hours until the alarm is desired.

        intSecondsUntilAlarm = DateDiff(DateInterval.Second, myTime, myAlarmTime)

        Timer1.Interval = 1000
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        intSecondsUntilAlarm -= 1
        Label1.Text = intSecondsUntilAlarm
        If intSecondsUntilAlarm = 0 Then
            'Sound the Alarm!
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Timer1.Enabled = True
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Timer1.Enabled = False
    End Sub
 
thanks man I'll give it a try, but ppoint of this is not really the alarm time I'm doing a timer for the game, so the focus is on stoping it and continuing it correctly as well as displaying it min:sec.


thanx
 
No problem. Rather than "sound the alarm" you would just run your checking function at that point. As for formatting the seconds as min:sec some simple math can get that back out for you.

Pseudo:

minutes = totalsecondsleft / 60
seconds = totalsecondsleft MOD 60

MOD returns the remainder of a division action so:

if totalsecondsleft was 65 then
minutes = totalsecondsleft / 60 = 1
seconds = totalsecondsleft MOD 60 = 5

you would show minute:seconds and get 1:05
 
Back
Top