0

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.

Sub Main()
    Do
    Dim backupTime As String

    backupTime = My.Settings.backupTime


    While TimeOfDay = backupTime
        Threading.Thread.Sleep(2000)
    End While


    backup()
  loop
End Sub

The "backupTime" that is being referred to is stored in the App.Config file

<userSettings>
    <ZipAJob_Timed.My.MySettings>
        <setting name="backupTime" serializeAs="String">
            <value>#01:59:00 PM#</value>
        </setting>
    </ZipAJob_Timed.My.MySettings>
</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.

MaylorTaylor
  • 4,671
  • 16
  • 47
  • 76
  • Have you considered making an app that just does the task once and quits, then adding it as a scheduled task (using the Task Scheduler under your Computer Management on windows)? – Steven Doggart Feb 22 '13 at 19:43
  • Yea, that's the easy way out. i'm trying to learn something new. – MaylorTaylor Feb 22 '13 at 19:53

1 Answers1

2

Why don't you simply use < instead of = ?

While TimeOfDay < backupTime
    Threading.Thread.Sleep(2000)
End While

But instead of a loop i would either use a Timer or a Windows-Task that starts at the given time.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This is a VB Console application so i'm not sure Timer is an option. And like i told Steven, i just wanna try something new. I'm still green at this whole VB thing, so i'm just trying everything the hard way once – MaylorTaylor Feb 22 '13 at 19:55
  • The `System.Windows.Forms.Timer` wouldn't be an option, but `System.Timers.Timer` would be an excellent option. – Steven Doggart Feb 22 '13 at 20:12
  • @MaylorTaylor: I strongly suggest to set `Option Strict` to ON. You will probably get many compiler errors then, but you will learn faster. Then you should change the type of your setting from string to `TimeSpan`. http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/be60552d-d155-449d-b135-4770c5aa271c/ – Tim Schmelter Feb 22 '13 at 20:21
  • Ultimately i would like this console program to just run continuously and each time the While statement rings true, it goes to the [code]backup()[/code]. I only want it to run once though, so having the [code]While TimeofDay < backupTime[/code] won't help. – MaylorTaylor Feb 22 '13 at 20:30
  • @MaylorTaylor: If you want to highlight code in comments: http://meta.stackexchange.com/questions/24793/how-to-use-formatting-in-comments A backup is really something that should be triggered from a windows-task, a loop with `Thread.Sleep` is not the right way http://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful – Tim Schmelter Feb 22 '13 at 20:38