If time equals to...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    If time equals to...

    I'm trying to have my console app program zip files from an XML file. That part is done; but now, to add more of a headache, i want this action to happen only at certain times.

    So, what i'm think is something similar to this

    Code:
    Dim clocktime = Format(TimeOfDay, "Long Time")
            If clocktime = #1:26:00 PM# Then
    and then add all of my code that normally would run. The problem with this method is that when i run the program, it immediately closes b/c it isn't that time. So, my question is...how do i have the program continously check for the time and only run the "if statement" at a certain time.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    You have not posted enough of your code for anyone to help...

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Use an infinite loop to continuously run the code.

      Also, put in a pause so that it's only checking every minute or so. You don't want it to actually check constantly.

      And your run condition really shouldn't be an exact time. Rather, it should run if it's past a certain time in case the process gets to the check one second after the run time. But of course that brings up another problem in that it will run continuously after that time. What you can do is put in a longer wait after it runs, or keep track of the last run date and only run if it hasn't yet for that day.

      I'm not sure how the program will react when comparing a string against a date so you may want to play on the safe side and have it return a 24-hour time instead.

      Comment

      • maylortaylor
        New Member
        • Nov 2012
        • 72

        #4
        Figured it out thanks to the manager.

        Code:
        Sub Main()
        
                If Now.Hour = 16 And Now.Minute = 57 Then
                    runBackup()
        
                End If
        
                Threading.Thread.Sleep(2000)
                Main()
            End Sub
        The
        Code:
        runBackup()
        code is where i have the actions to be done at the particular time.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You don't want to do it that way. At some point you're going to overflow. Use an infinite loop instead. Calling a function infinitely is dangerous.

          And you really should run the time check in the way I suggested in post #3. If for some reason the thread gets pushed down in the priority far enough or if the CPU gets hung up for even a minute, the update may end up not running.
          Last edited by Rabbit; Dec 6 '12, 11:19 PM.

          Comment

          Working...