-1

I am programming in VB.NET and can't get my head around a problem I have.

I have a timer that starts on the nth minute of the hour, eg site number 7 starts on the 7th minute, site number 56 starts on the 56th minute, site number 70 starts on the 10th minute (modulus).

SO I need to identify when my service starts, whether i can test to see if the timer should be running or not.

So for site 45, this would start on the 45th minute of the hour. If the service starts at 10:55, I would not test this timer until 11:45

My head is numb from trying to think about this too much, so any help would be great.

I just need a function to test whether the site timer is running or not, based on the offset hour.

  • 2
    Why don't you use the Windows Task Scheduler? – Tim Schmelter Jan 16 '18 at 15:27
  • Possible duplicate of [How to find out if a Timer is running?](https://stackoverflow.com/questions/150575/how-to-find-out-if-a-timer-is-running) – Trevor Jan 16 '18 at 15:42
  • Your question is unclear. You describe a ölogical problem but your title and the last sentence just mentions that you need to check whether a timer is currently running or not. What has this to do with the rest of your question? – Tim Schmelter Jan 16 '18 at 15:49
  • My terminology possibly wasnt great. The timer runs in vb.net application and fires every 10 seconds. However, it cannot start firing until (for example) the 15th minute of the hour. I have another timer which checks the status of the first timer, and must be able to calculate from the start time of the vb application, and the calculated offset of the timer, whether or not the first timer should be running. All this is done within a single application – Lord Nodral III Jan 18 '18 at 16:56

2 Answers2

1

Here is a reference URL HERE that someone already asked, but the short looks to be; Check:

If timer.enabled = true Then
'''Do stuff etc...
End If
Michael Edmison
  • 663
  • 6
  • 14
1

Instead of fiddling around with timers and constantly checking if your service needs to start, why don't you use the Windows Task Scheduler?

However, here is a try to solve your problem. You have an integer(your service-ID) and you want to check when it needs to start according to your business rule:

Public Function WhenTimerHasToStart(serviceId As Int32) As Date
    Dim minuteRemainder = serviceId Mod 60
    Dim now = Date.Now
    Dim dt = New Date(now.Year, now.Month, now.Day, now.Hour, minuteRemainder, now.Second)

    If dt >= now Then
        Return dt
    Else
        Return dt.AddHours(1)
    End If
End Function

Now you know when the timer has to start, so the service can use timer.Change(if Threading.Timer) with the correct parameter. For example:

Dim serviceId = 70
Dim timerStart = WhenTimerHasToStart(serviceId) ' 1/16/2018 05:10:17 PM    
Dim offsetMillis As int32 = CInt((timerStart - Date.Now).TotalMilliseconds)
timer.Change(offsetMillis, Threading.Timeout.Infinite)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939