By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

If time equals to...

maylortaylor
P: 72
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

Expand|Select|Wrap|Line Numbers
  1. Dim clocktime = Format(TimeOfDay, "Long Time")
  2.         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.
Dec 6 '12 #1

✓ answered by maylortaylor

Figured it out thanks to the manager.

Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  2.  
  3.         If Now.Hour = 16 And Now.Minute = 57 Then
  4.             runBackup()
  5.  
  6.         End If
  7.  
  8.         Threading.Thread.Sleep(2000)
  9.         Main()
  10.     End Sub
The
Expand|Select|Wrap|Line Numbers
  1. runBackup()
code is where i have the actions to be done at the particular time.

Share this Question
Share on Google+
4 Replies


zmbd
Expert Mod 5K+
P: 5,397
You have not posted enough of your code for anyone to help...
Dec 6 '12 #2

Rabbit
Expert Mod 10K+
P: 12,382
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.
Dec 6 '12 #3

maylortaylor
P: 72
Figured it out thanks to the manager.

Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  2.  
  3.         If Now.Hour = 16 And Now.Minute = 57 Then
  4.             runBackup()
  5.  
  6.         End If
  7.  
  8.         Threading.Thread.Sleep(2000)
  9.         Main()
  10.     End Sub
The
Expand|Select|Wrap|Line Numbers
  1. runBackup()
code is where i have the actions to be done at the particular time.
Dec 6 '12 #4

Rabbit
Expert Mod 10K+
P: 12,382
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.
Dec 6 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.