Call a sub at specific time twice a day

JonathanP

Member
Joined
Nov 7, 2012
Messages
8
Programming Experience
Beginner
Apologies if this has been covered before but I am trying to get a sub to run twice a day at 05:30 and 17:30 everyday. Would any one be able to assist. I had it running on a spreadsheet with VBA but I am now tring to get it to run in a datagridview in a VB.NET form.

Thanks
 
Hi,

You just need to add a Timer with its interval set to, say, 1 minute (60,000 milliseconds). Then, in it's tick event you can check for the required times that you need and the just call your Sub when your times are reached.

Hope that helps.

Cheers,

Ian
 
Hi,

You just need to add a Timer with its interval set to, say, 1 minute (60,000 milliseconds). Then, in it's tick event you can check for the required times that you need and the just call your Sub when your times are reached.

Hope that helps.

Cheers,

Ian

Why set it to one minute? Why not set it to the exact amount of time until you next want to perform an action? A Timer can go to about 24.5 days between Ticks. I haven't tested the accuracy of a Timer with those sorts of Intervals but that would be the first choice I would think. If it tends to drift a bit then set it to Tick a bit before the actual time and then on the first Tick set it to the actual time. It won't drift over the course of a few minutes.
 
Hi jmcilhinney,

You could do it your way but you have assumed that the routine could never and would never end and JonathanP does not indicate that this application would never close. By doing it the way I posted the routine could start and end at any point in time and always return an action at the specified time. If you are suggesting something I have missed please explain further.

Cheers,

Ian
 
Hi,

I thought about jmcilhinney's comments above and you could do it this way. If you define some sort of list of the time's you need something to run then you could use this example to achieve this:-

VB.NET:
Public Class Form1
  Dim lstActivationTimes As New List(Of DateTime)
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    With lstActivationTimes
      .Add(DateTime.Parse("08:00:00 AM"))
      .Add(DateTime.Parse("05:30:00 PM"))
    End With
    SetNextOccuranceOfTimer()
    Timer1.Enabled = True
  End Sub
 
  Private Sub SetNextOccuranceOfTimer()
    Dim TodaysTimeActvated As Boolean
 
    For Each lstTime As DateTime In lstActivationTimes
      If lstTime.TimeOfDay >= Now.TimeOfDay Then
        Timer1.Interval = CInt(lstTime.TimeOfDay.Subtract(Now.TimeOfDay).TotalMilliseconds)
        TodaysTimeActvated = True
        Exit For
      End If
    Next
    If Not TodaysTimeActvated Then
      Dim EndOfDay As DateTime = DateTime.Parse("23:59:59 PM")
      Dim StartOfDay As DateTime = DateTime.Parse("00:00:00 AM")
 
      For Each lstTime As DateTime In lstActivationTimes
        If lstTime.TimeOfDay >= StartOfDay.TimeOfDay Then
          Timer1.Interval = CInt(EndOfDay.TimeOfDay.Subtract(Now.TimeOfDay).TotalMilliseconds) + CInt(lstTime.TimeOfDay.Subtract(StartOfDay.TimeOfDay).TotalMilliseconds)
          Exit For
        End If
      Next
    End If
  End Sub
 
  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    'run your code here
    SetNextOccuranceOfTimer()
  End Sub
End Class

The tricky thing was dealing with the change of days, but that is accommodated here and you must remember to add the times of things to happen in order of time.

Hope that helps.

Cheers,

Ian
 
get a sub to run twice a day at 05:30 and 17:30 everyday.
    Private Sub SetNextOccuranceOfTimer()
        Dim setDate = Date.Parse("05:30")
        Do Until Date.Now < setDate
            setDate = setDate.AddHours(12)
        Loop
        Timer1.Interval = CInt((setDate - Date.Now).TotalMilliseconds)
        Timer1.Start()
    End Sub

Timer Tick event handler is the sub that is called at that time.
 
Back
Top