Setting a timer to run on the hour

Posted on 2011-10-24
Last Modified: 2012-05-12
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
Question by:thoecherl
    LVL 25

    Expert Comment

    by:Luis Pérez
    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.
    LVL 83

    Expert Comment

    Every second would be a bit extreme. I would set it to every minute.
    LVL 85

    Accepted Solution

    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


    Author Closing Comment

    Perfect, Idle_Mind.  Thank you.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now