question

WilfredEghenedji-9495 avatar image
0 Votes"
WilfredEghenedji-9495 asked WilfredEghenedji-9495 commented

Continue countdown from last count, when app is stopped and restarted

I am trying to create a countdown that if the app is shutdown while it is still counting, it should continue from last count when the app is restarted. For instance: I have a countdown which counts down from 360 to 0. If the countdown is currently in 120, and the app is shutdown, i would like the countdown continues from 120 instead of starting all over from 360, whenever the app is restarted. So far, i have the following code. The countdown works but the My.Settings.countdown doesn't seems to work. When i stopped the app and restarted it, it doesn't start from where the counting was when i aborted the app. Someone please help fix this. Thank you very much for the timely response and past assistance.

 Public Class frmMain
    
    Dim WithEvents Timer5 As New System.Windows.Forms.Timer     'Make a new timer    
    Dim TimerCounter2 As Integer = 3600     'Starting Value
    
 Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load    
    lblCoutdownReader2.Text = My.Settings.countdown    

    lblCoutdownReader2.Text = CStr(TimerCounter2)
    Timer5.Interval = 1000      'Set the interval (milliseconds)
    Timer5.Start()   
    
    If lblCoutdownReader2.Text = 0 And TxtActivationKeyStore.Text <> "6564WEDR3432GFD657" Then
             PanelAppActivation.Visible = True
    End If 
 End Sub   
    
 Private Sub Timer5_Tick(sender As Object, e As EventArgs) Handles Timer5.Tick  
         TimerCounter2 -= 1             'DeIncrement the counter
         lblCoutdownReader2.Text = CStr(TimerCounter2)           'Show the counter
         If TimerCounter2 = 0 Then       'Countdown reached 0
             Timer5.Stop()   'Stop the Timer          
             PanelAppActivation.Visible = True
             Button101.Enabled = False 
         End If
 End Sub 
    
 End Class           


dotnet-visual-basicwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered WilfredEghenedji-9495 commented

Try adding these lines after decrementing the counter in Timer5_Tick:

 My.Settings.countdown = TimerCounter2 
 My.Settings.Save( )

Also fix the definition of TimerCounter2:

 Dim TimerCounter2 As Integer = My.Settings.countdown

Set the initial value to 3600 manually in Settings dialog.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much! This was really helpful!

0 Votes 0 ·

i stumbled on an issue: "When the countdown counts down to zero, and the app is shutdown and re-started, it starts counting -1, -2, -3.... instead of re-starting the countdown all over from the assigned value (3600). I tried using another timer to re-start the countdown from 360, but all to no avail". What really could be issue, please!

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 WilfredEghenedji-9495 ·

If your code contains

Dim TimerCounter2 As Integer = My.Settings.countdown

then maybe replace it with

Dim TimerCounter2 As Integer = If(My.Settings.countdown <= 0, 3600, My.Settings.countdown)


0 Votes 0 ·

Yeah, this solves the problem. Thank you.

0 Votes 0 ·
AdnanDedic avatar image
0 Votes"
AdnanDedic answered AdnanDedic commented

Hi,
You can save current value on exit and, load it at startup.

Here is the example that uses application settings, but you can really save it wherever you want (registry, database, filesystem, etc.). And you can also use different events (OnApplicationExit, etc).

122079-capturesettings.jpg

 Public Class Form1
    
     Dim WithEvents Timer As New System.Windows.Forms.Timer     'Make a new timer    
     Dim TimerCounter As Integer    'Timer counter variable
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         TimerCounter = My.Settings.LastTimerCounter 'Load last timer counter from the settings
         Timer.Interval = 1000      'Set the interval (milliseconds)
         Timer.Start()
     End Sub
    
     Private Sub Timer5_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
         TimerCounter -= 1             'DeIncrement the counter
         Label1.Text = TimerCounter
     End Sub
    
     Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
         My.Settings.LastTimerCounter = TimerCounter
         My.Settings.Save()
     End Sub
 End Class

BR,
Adnan



capturesettings.jpg (139.8 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, i attempted this too, and it worked.

0 Votes 0 ·
AdnanDedic avatar image AdnanDedic WilfredEghenedji-9495 ·

You are welcome

0 Votes 0 ·