Results 1 to 10 of 10

Thread: I have a question about timer.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    3

    Question I have a question about timer.

    Hello, guys, I'm new in this forum first of all nice to meet you guys. I have question about timer in VB.NET so I have a countdown program and I did everything good the countdown timer is work perfectly but I want to add something like when I close the program I wanna my timer still continue and if I restart my PC I want to my timer still work and when I open my program again after restarting my PC I want to my timer still continue for example

    I put 5 hours for the countdown and if I close my PC or program for 3 hours and I open again my PC or program I want to my timer continue countdown from 2. if you guys can help me I will get happy thanks a lot sorry for bad grammar.

    also, this is my codes:
    Code:
    Public Class Form1
    
        Private TargetDT As DateTime
        Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(601)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
    
            Timer1.Interval = 1000
            TargetDT = DateTime.Now.Add(CountDownFrom)
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            '  Label1.Text = Label1.Text - "1"
            Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
            If ts.TotalMilliseconds > 0 Then
                Label1.Text = ts.ToString("hh\:mm\:ss")
    
    
            Else
                Label1.Text = "00:00:00"
                Timer1.Stop()
                MessageBox.Show("Done")
            End If
    
        End Sub
    End Class

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,623

    Re: I have a question about timer.

    You would need to save your targetDT to a file then load the value for it when the program loads, or perhaps you would need to save the time elapsed to the file and load that depending on if you want the timer suspended while the program is not running so that it picks up where it left off or you want the time to be from the original time no matter if the program was stopped or not

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    3

    Re: I have a question about timer.

    hey brother I don't wanna suspend the program I want to the program still contiune how can I do that do you have any source or code ?

  4. #4
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: I have a question about timer.

    Quote Originally Posted by byani View Post
    hey brother I don't wanna suspend the program I want to the program still contiune how can I do that do you have any source or code ?
    It is not possible to have a close program and continue to do something in the program. Now it is possible to have the timer and it's code in a background program without a user interface that will keep running (not closed) when your main "control" program is closed. In this case you would essentially need two programs. The main user interface "control" program that sets values and starts the timer and the background program that reads the values and contains the timer and it's functionality. But this is going to be a convoluted mess.

    It is also possible, as DataMiser mentioned to write the timer and elapsed time values somewhere and have your background program read them when it starts back up after a reboot (and even have your background program auto-run after a reboot) and the set the timer to the remaining time. BUT there is absolutely no way to have a program run while the computer is turned off or keep actively counting down during the actual reboot process.

    Probably the best way to accomplish this is to not use the timer in the way you are thinking. Instead of setting a timer to 5 hours, when you want something to happen in 5 hours, look at the current time, add 5 hours and save that future time value somewhere. Then have your timer set to one second or one minute (depending on precision of timing you need) and compare the current time to the saved target time value. When they match OR current time is later than the target time do something and clear the saved time value (to prevent multiple triggers). This method will allow reboots to not interfere if you program restarts after the reboot and will allow the program to be closed while waiting. However, if the program is closed or the computer is turned off when the target time is reached, nothing will happen until the program is started again.

    It might help if we knew the purpose of such code. What are you actually trying to do here.

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,369

    Re: I have a question about timer.

    To "keep the timer running" when you turn off the pc you will need to store the last or next execution time somehow so it knows what time it will next execute...

    For example, when the programs timer executes and the timer executes every 5 hours, store the next time of the execution on the tick...
    This way if your program is closed, you can read the next timer tick setting when your app loads...
    Then set the initial timer to be the difference between the current time and the next tick...
    If the stored time is past you will need to do something, either work out when the next tick will be based on that, or execute your timer at that time.
    When the timer ticks it will need to set its interval after this happens, as it would have been adjusted.

    As for your app starting, if you want it to start at login, there are many examples of this if you search.

    Kris

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    3

    Re: I have a question about timer.

    thanks I mean do you know some programs have timer like some games when u login to program it says you have 5 hours for next prize and after 1 hours when you login again its says 4 hours more left something like that I want to do like that.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,301

    Re: I have a question about timer.

    I believe you've been told more than once how to do that.
    You need to change the code so that it will read the TargetDT from somewhere, like a file, so your program will continue counting down to that time when it starts up.
    I assume you'll want to add something to the GUI so that the time can be set, rather than hard coded in the form load event.

    If it were me, I would use an Application Settings DateTime value which you can access through My.Settings and different users of the program (assuming they login to windows with different account names), would automatically have a DataTime unique to their run of the program.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: I have a question about timer.

    Quote Originally Posted by byani View Post
    thanks I mean do you know some programs have timer like some games when u login to program it says you have 5 hours for next prize and after 1 hours when you login again its says 4 hours more left something like that I want to do like that.
    Games I have seen like that, that you log into, all of those timers are handled server side, not on the game client.

  9. #9
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,369

    Re: I have a question about timer.

    Quote Originally Posted by Maverickz View Post
    Games I have seen like that, that you log into, all of those timers are handled server side, not on the game client.
    Yes a lot do ... but it is really surprising how many don't, Fallout Shelter comes to mind, subway surfer didn't use to... but I know I have found others too.

    Kris

  10. #10
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: I have a question about timer.

    Quote Originally Posted by i00 View Post
    Yes a lot do ... but it is really surprising how many don't, Fallout Shelter comes to mind, subway surfer didn't use to... but I know I have found others too.

    Kris

    Well if this is a game and you handle the timers client side, then to help avoid cheating you would need to encrypt the values. Otherwise the player could just find the file and change the values. This is why so many games handle it server side.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width