3

Based on this SO post (https://stackoverflow.com/questions/1329900/net-event-every-minute-on-the-minute-is-a-timer-the-best-option) is it possible to create a timer event that fires every 30 seconds but starts on the 15 second mark according to the system clock?

So 00:15, 00:45, 01:15, 01:45, etc.

UPDATE

To be more specific, I want to enable a timer that synchronizes to the Windows system clock. At every 15 seconds, I want to run some code, then at every 45 seconds I want to run the code again. I need this event to fire on the exact 15 second mark and 45 second mark according to the system time, not just every 30 seconds from when the timer started.

So if I start the timer at 12:30:47 I want the timer to fire at 12:31:15 and again at 12:31:45 and so on.

Community
  • 1
  • 1
Riples
  • 1,167
  • 2
  • 21
  • 54
  • How do I get down voted for that? I thought the question was pretty clear?? – Riples Dec 07 '14 at 02:06
  • It is perfectly clear to me, and Lajos Arpad certainly understood it will enough to provide a good answer. – xpda Dec 07 '14 at 02:40

2 Answers2

2

How about:

Dim startin as Integer = 15 - DateTime.Now.Second
If (startin < 0) Then
    startin = startin + 50
End If
Dim t as System.Threading.Timer = new System.Threading.Timer(AddressOf tick, _
     Nothing, startin * 1000, 30000)

Sub tick(ByVal status As Object)
Console.WriteLine(Now) ' or MsgBox(Now)
End Sub

My Visual Basic is very rusty, so if I have made typos, please, let me know.

xpda
  • 15,585
  • 8
  • 51
  • 82
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
1

Make a timer and set the interval to 1000 "1 second".
Then make a dim isec as integer.
This you wil use to check the system time"seconds".

Then make a if statement and check if isec is 15 or 45 .

Dim isec As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
isec = DateTime.Now.Second

If isec = 15 Or isec = 45 Then
'Your code
End If

End Sub
Creator
  • 1,502
  • 4
  • 17
  • 30
  • How would this timer align with the system clock to start at exactly 15 seconds past? – Riples Dec 07 '14 at 02:17
  • @Riples That is what my code is doing. DateTime.Now.Second is the system time "so with my code you check the system time seconds and when it 15 or 45 the if statment wil run the code" – Creator Dec 07 '14 at 02:56
  • Thankyou - your example gave me exactly what I needed. – Riples Dec 12 '14 at 03:31