none
How can i sync and save time? More in here... RRS feed

  • Question

  • Hello guyz

    I want to ask how can i make the application is still countdown while the appliction is closed.

    Uhh, so i want to create an application that focus on countdown.

    So i start the 10 min countdown and close the application and i open after 6 minutes and the time is still countewd down while application is closed.

    i want to do whit pc time. but how can make it happen?

    Thanks for everys reply.

    Tuesday, October 3, 2017 5:25 PM

All replies

  • Hi

    Maybe something like this. Needs a TextBox1, Timer1 and Button1

        Dim Start As Integer = 0
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Me.Hide()
            Start = 600
            TextBox1.Text = Start.ToString
            Timer1.Interval = 1000
            Timer1.Enabled = True
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Start -= 1
            TextBox1.Text = Start.ToString
            If Start < 1 Then
                Me.Show()
                Beep()
                Timer1.Enabled = False
            End If
        End Sub


    Regards Les, Livingston, Scotland



    • Edited by leshay Tuesday, October 3, 2017 5:51 PM
    Tuesday, October 3, 2017 5:50 PM
  • okay, but i want to do this just whit the application is fully closed and example the pc is turned off

    any ideas?

    Tuesday, October 3, 2017 5:52 PM
  • Hi

    Do I understand correctly. Say you start the Timer countdown at 10min and after (say) 2 min you shut down the application and then (say) 20 min later you re-run the application - do you want the countdown to continue with 8 min to go?

    If so, then in the Form Close event, save the current countdown value to (say) a My.Settings vaiable then in the Load event, check the saved value and if greater than zero, recommence the countdown with that value.

    For example, something like this.

    '  Form1 with Timer1, TextBox1, Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Start As Integer = 0
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            My.Settings.CountDown = Start
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If My.Settings.CountDown > 0 Then
                StartCountDown(My.Settings.CountDown)
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            StartCountDown(4)
        End Sub
        Sub StartCountDown(s As Integer)
            Start = s
            TextBox1.Text = Start.ToString
            Timer1.Interval = 1000
            Timer1.Enabled = True
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Start -= 1
            TextBox1.Text = Start.ToString
            If Start < 1 Then
                Beep()
                Timer1.Enabled = False
                My.Settings.CountDown = 0
            End If
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 3, 2017 6:13 PM
    Tuesday, October 3, 2017 6:04 PM
  • Hi no i want like this i start the 40 min countdowan close after 2 min and i open after 20 min but while the application close the coundown is not stopped, and i open app and the reaming time 18 min.

    Thats i want

    Tuesday, October 3, 2017 6:12 PM
  • Hi no i want like this i start the 40 min countdowan close after 2 min and i open after 20 min but while the application close the coundown is not stopped, and i open app and the reaming time 18 min.

    Thats i want


    Can you just save the time to disc when you close and then when the app starts again read the time and compare to now and whalaa?
    Tuesday, October 3, 2017 6:16 PM
  • yeah yeah thats it but i cant imagine how, can you help me please?
    Tuesday, October 3, 2017 6:17 PM
  • yeah yeah thats it but i cant imagine how, can you help me please?

    What do you know how to do already?

    Are you using my.settings?

    Are you familiar with the Date and DateTime classes (or whatever they are called).

    Using My settings you can save the time when you close, and then when your app starts you read the my.settings for your time variable and subtract from the current time. Something like this:

        My.settings.mytimevariable - NOW = elapsed time

        if elapsedtime < 40 then

            'do the thing

        else

            'whatever

        End

    That's not real code. You will have to look them up and try and then come back with questions.

    :)

    Tuesday, October 3, 2017 6:22 PM
  • okay i can use for mysettings? i dont know, but so i already know how to use mysettings but i dont know i can do that.

    thnak i give a try

    Tuesday, October 3, 2017 6:26 PM
  • uhhh, i have a problem i use this code for countdown and i have no idea what is the value of the time:

    //This is the setup
    Private TotalTime1 As TimeSpan
    Private TotalTime2 As TimeSpan
    
    //This is the start
    TotalTime1 = New TimeSpan(990000000000)
    Timer1.Enabled = True
    TotalTime2 = New TimeSpan(9000000000)
    Timer2.Enabled = True
    
    //The countdown(s)
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If TotalTime1.Ticks <= 0 Then
                Timer1.Enabled = False
                Label3.Text = "0:00:00:00"
            End If
            TotalTime1 = TotalTime1.Subtract(TimeSpan.FromMilliseconds(Timer1.Interval))
            Label3.Text = String.Format("{0:0}:{1:00}:{2:00}:{3:00}", TotalTime1.Days, TotalTime1.Hours, TotalTime1.Minutes, _
                TotalTime1.Seconds)
        End Sub
    
    ---------------------------------------------------
    
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If TotalTime2.Ticks <= 0 Then
                Timer2.Enabled = False
                Label4.Text = "0:00:00:00"
            End If
            TotalTime2 = TotalTime2.Subtract(TimeSpan.FromMilliseconds(Timer2.Interval))
            Label4.Text = String.Format("{0:0}:{1:00}:{2:00}:{3:00}", TotalTime2.Days, TotalTime2.Hours, TotalTime2.Minutes, _
                TotalTime2.Seconds)
        End Sub

    Tuesday, October 3, 2017 6:34 PM
  • okay i can use for mysettings? i dont know, but so i already know how to use mysettings but i dont know i can do that.

    thnak i give a try

    Oh, well I guess that is basically what Leshay showed if you just modify it slightly.

    I did not look at his example close enough. See the my.settings?

    Tuesday, October 3, 2017 6:36 PM
  • oh sorry i dont look enought clore thats my mistake

    but in my code what is the time value?

    Tuesday, October 3, 2017 6:39 PM
  • Hi

    Here is a hurried example, try it out. Needs 2 My.Settings, 1: CountDown as type Long and 2: ShutDownTime as Date

    '  Form1 with Timer1, TextBox1, Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Start As Long = 0
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            My.Settings.CountDown = Start
            My.Settings.ShutDownTime = Now
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If My.Settings.CountDown > 0 Then
                Dim dif As Long = DateDiff(DateInterval.Second, My.Settings.ShutDownTime, Now)
                StartCountDown(My.Settings.CountDown - dif)
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            StartCountDown(600)
        End Sub
        Sub StartCountDown(s As Long)
            Start = s
            If Start > 0 Then
                TextBox1.Text = Start.ToString
                Timer1.Interval = 1000
                Timer1.Enabled = True
            Else
                Start = 0
            End If
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Start -= 1
            TextBox1.Text = Start.ToString
            If Start < 1 Then
                Beep()
                Timer1.Enabled = False
                My.Settings.CountDown = 0
            End If
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, October 3, 2017 6:51 PM
  • oh sorry i dont look enought clore thats my mistake

    but in my code what is the time value?

    In your code you only have the elapsed time span. Call it dt ie Difference in Time.

    That's fine for a countdown timer. However you now want to close and restart the app so you will have to save the time of day. You can use a date variable in my.settings and save the time when you close t1 then when you start up you get the current time (now) t2. So that data will be datetime structures. Like integer or double but datetime (or date too). 

    https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

    You need to learn how they work. You may need to make the examples and play around just learning to use the date and the my.settings.

    Then you make your my.setting variable as date. Then you subtract the two times (using date subtract). After you do it all correctly it will look like this

      Dim t2 as date = NOW

      Dim dt as timespan = t2 - my.settings.t1

    Not real working code.





    Tuesday, October 3, 2017 6:52 PM