Setting a timer to run on the hour

I have a program that runs and accomplishes several tasks every 60 minutes.  I am satisfied with how it is working, but it runs every hour from the time I launch the application.  If I launch the application at 8:50 am, it runs then and again at 9:50, etc.  I would like it to run at 9:00, 10:00, etc.  Is there a way to accomplish this with
T HoecherlDeveloperAsked:
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.

Luis PérezSoftware Architect in .NetCommented:
Set the Interval property of the Timer to 1000 (it will check every 1 second).

In the code, write this:
Private Sub Timer1_Timer(sender As Object, e As System.EventArgs)
    If Date.Now.ToString("HH:mm:ss").EndsWith(":00:00") Then
        'Start the Task
    End If
End Sub

Hope that helps.
Every second would be a bit extreme. I would set it to every minute.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like...
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = MillisecondsToNextHour()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' ... do something in here ...
        Debug.Print("Tick() Event @ " & DateTime.Now)

        Timer1.Interval = MillisecondsToNextHour()
    End Sub

    Private Function MillisecondsToNextHour() As Double
        Dim dt As DateTime = DateTime.Today.AddHours(DateTime.Now.Hour).AddHours(1)
        Debug.Print("Next Tick @ " & dt)
        Return dt.Subtract(DateTime.Now).TotalMilliseconds
    End Function

End Class

Open in new window


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
T HoecherlDeveloperAuthor Commented:
Perfect, Idle_Mind.  Thank you.

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.