VB.net Run code at certain time today

Hi

If I have the time in a string (eg 11:00), and I turn my computer on I want to set a timer to run certain code on that hour.
What VB.net code do I use to do this?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
You have two options that I can think of.

1. Create a simple console application and run it as a scheduled task.
2. Create a Windows service application and include logic that executes the code at a scheduled interval.

The first is much easier to implement but relies on the Windows Task Scheduler. The second takes quite a bit more setup but has no external dependencies. There are many articles explaining how to create a service. A couple of the better ones are:

http://www.devguru.com/features/tutorials/windows_srv/windows_service
http://www.codeproject.com/Articles/106742/Creating-a-simple-Windows-Service
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Assuming you set your VB.Net application to be run at startup somehow (and you plan on leaving it RUNNING, whether visible or not until the desired time), you'd simply parse the time and setup a DateTime variable to hold your target date/time (be it today if that time has not occurred yet, or tomorrow at that time if we are already past the desired time of day).

Once you've got your target DateTime you have two basic options:
(1) Poll the current date/time to see if we are past the target date/time
(2) Calculate the duration of time to the target date/time and put some kind of thread to "sleep" for that amount of time.

An example of (1), which is useful, for example, if you need to display some kind of progress towards the target time or a countdown:
Public Class Form1

    Private targetTime As DateTime
    Private scheduledTime As String = "11:00"
    Private WithEvents Tmr As New System.Windows.Forms.Timer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Tmr.Interval = 1000
        Tmr.Enabled = False
        SetTarget()
    End Sub

    Private Sub SetTarget()
        ' allow various time formats and attempt to parse the scheduled time: (with or without leading zeroes, allow am/pm or 24 hour format)
        Dim allowed() As String = {"h:mm", "hh:mm", "h:mm tt", "hh:mm tt", "H:mm", "HH:mm"}
        If DateTime.TryParseExact(scheduledTime, allowed, Nothing, Globalization.DateTimeStyles.None, targetTime) Then
            If targetTime < DateTime.Now Then
                ' we're already past the target time, schedule it for tomorrow at the same time:
                targetTime = targetTime.AddDays(1)
            End If
            Tmr.Start()
        End If
    End Sub

    Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
        Dim ts As TimeSpan = targetTime.Subtract(DateTime.Now)
        If ts.TotalMilliseconds <= 0 Then
            Tmr.Stop()
            MessageBox.Show("Target Time Reached!")

            ' set it up again?
            SetTarget()
        Else
            ' display time remaining?
            Label1.Text = "Alarm Time: " & scheduledTime
            Label2.Text = "Current Date/Time: " & DateTime.Now
            Label3.Text = "Time Remaining: -" & ts.ToString("hh\:mm\:ss")
        End If
    End Sub

End Class

Open in new window


Screenshot:
Polling for a Target Time
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.