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

checking TimeofDay against app setting value

P: 72
I'm writing a VB console program that will run a function at a certain time of day. This is what i have so far.
Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  3.     Dim backupTime As String
  5.     backupTime = My.Settings.backupTime
  8.     Do Until TimeOfDay = backupTime
  9.         Threading.Thread.Sleep(2000)
  10.     Loop
  12.     backup()
  13. End Sub
The "backupTime" that is being referred to is stored in the App.Config file

Expand|Select|Wrap|Line Numbers
  1. <userSettings>
  2.     <ZipAJob_Timed.My.MySettings>
  3.         <setting name="backupTime" serializeAs="String">
  4.             <value>#01:59:00 PM#</value>
  5.         </setting>
  6.     </ZipAJob_Timed.My.MySettings>
  7. </userSettings>
The problem is that the "Do While" loop checks every 2 seconds for the "TimeofDay" to match "backupTime". But that can lead to the program not running because it missed the match by 2 seconds. So either i need to have it disregard the seconds or to have the TimeofDay.minutes/hours and backupTime.minutes/hours match only.

I've looked into Parsing..but not exactly sure where i should start.
Feb 22 '13 #1
Share this Question
Share on Google+
3 Replies

P: 94
You should be comparing dates rather than string values. You could bump your time up to every minute and check that.

Expand|Select|Wrap|Line Numbers
  1.    Dim backupTime as DateTime = Date.Parse(My.Settings.backupTime)
  2.    Do
  3.        If DateDiff(DateInterval.Minute, backupTime, Now) = 0 Then
  4.            ' This will trigger when a match is made by the minute
  5.        Else
  6.            ' Sleep for 60 seconds
  7.            Threading.Thread.Sleep(60000)
  8.        End If
  9.    Loop
You could also throw a timer on your form and set the interval. This way your app isn't constantly sleeping.
Feb 23 '13 #2

Expert Mod 5K+
P: 5,397
agreed on the string vs. date


... instead of looking for just "=" you should be looking the current date/time ">=" to your desired backup time. This way even if the comparison fails, the next step is executed even if the current time is past what you expected.

It depends on your application requirements; however, every 2 seconds seems excessive... imho... even every minute seems too much. Personally, I've never set a backup check for under 5 minutes or on exit of the program. The more frequent the check, the more likely you are to interfere with an end-user.
Feb 23 '13 #3

P: 72
I figured out how to solve the problem this way
Expand|Select|Wrap|Line Numbers
  1. Do
  3.             If ((Now.Minute() = backupTime.Minute) And (Now.Hour() = backupTime.Hour)) Then
  4.                 backup()
  5.             Else
  6.                 Threading.Thread.Sleep(15000)
  7.             End If
  8.         Loop
Feb 25 '13 #4

Post your reply

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