MadsTheMan MadsTheMan - 7 months ago 14
Vb.net Question

vb.net How can I execute a task between certain hours only

I have a private sub

Count_Average
that does stuff and I have a timer
TimerAverage
that is going to execute
Count_Average
. Right now its executing each minute, 24/7 - but I want it to only execute between certain hours only. Which would be between 07:00 - 23:59:59. How can I set that "propertie"?

Below is what my TimerAverage.tick does at the moment and also what I'm trying to accomplish.

Private Sub TimerAverage_Tick(sender As Object, e As EventArgs) Handles TimerAverage.Tick

Dim dateValue As DateTime = DateTime.Now
Dim startTime As DateTime = dateValue.Date + New TimeSpan(07, 0, 0)
Dim endTime As DateTime = dateValue.Date + New TimeSpan(23, 59, 0)

''''' Below is a demonstration of what I want (which obviously don't work)

''''' If not datetime.now is between starttime
' Do nothing

'else

TimerAverage.Stop()
Count_Average()
TimerAverage.Start()

'end if


I know this is a bad attempt, its more like a demonstration of what I'm trying to accomplish. I've tried to figure out how to write this correctly, but I can't figure it out.

Answer

If the range is 7:00 - 23:59 you could simply use:

If DateTime.Now.Hour >= 7 Then
    'do something
End If