Setting a timer

C

Crirus

This is more a logical problem than a VB.

I have this situation:

A timer that need to tick at each 10 minutes starting on minute 15 of curent
hour.

But I want to calculate the next tick time whenever I want to start the
timer.

So, I have ticks on minutes 25, 35, 45, 55, 05

If I start the timer on minute 33 I need that 2 minutes interval untill 35
in order to start the timer..
On tick event I reset the interval to 10 minutes.
How to calculate that 2 minute interval?
 
O

One Handed Man [ OHM# ]

PSEUDO - Hope this helps - Regards OHM#

Dim FirstTick As Boolean = True
Dim TimeNow as New DateTime = Now
Dim MinuteNow As Int32 = TimeNow.GetMinute
Dim InterimInterval As Int32

While MinuteNow < 15
MinuteNow = TimeNow.GetMinute
Loop

InterimInterval = 10 - ( MinuteNow MOD 10 )

Set Timer = InterimInterval

Sub On TimerTick

If FirstTick The
Set Timer Interval = 10
FirstTick = False
End If

End Sub












This is more a logical problem than a VB.

I have this situation:

A timer that need to tick at each 10 minutes starting on minute 15 of
curent hour.

But I want to calculate the next tick time whenever I want to start
the timer.

So, I have ticks on minutes 25, 35, 45, 55, 05

If I start the timer on minute 33 I need that 2 minutes interval
untill 35 in order to start the timer..
On tick event I reset the interval to 10 minutes.
How to calculate that 2 minute interval?

Regards - OHM# (e-mail address removed)
 
E

EricJ

2 remarks if i may ;p

1 i think timenow never gets a new value so the loop will never end (update
it in the while loop)

2 it looks like the while loop will run non stop, in the while loop call
doevents or a sleep
i don't think users would be happy when they click the button at 13:01 and
have to wait 14mins to do anithing w their pc

alternative could be working w 2 timers start the first one when the user
clicks set it for 1min intervals and when the MinuteNow < 15 is reached
launch the second one w 10min itervals and stop the first one.

hope it helps
 
O

One Handed Man [ OHM# ]

This was untested PSEUDO Code. The intention is clear even if the syntax is
not correct. When I write 'Actual' code, you can come back and criticise me
OK

Replace this with . . As PSEUDO
While MinuteNow < 15
MinuteNow = Now.GetMinute
Loop

OHM#
2 remarks if i may ;p

1 i think timenow never gets a new value so the loop will never end
(update it in the while loop)

2 it looks like the while loop will run non stop, in the while loop
call doevents or a sleep
i don't think users would be happy when they click the button at
13:01 and have to wait 14mins to do anithing w their pc

alternative could be working w 2 timers start the first one when the
user clicks set it for 1min intervals and when the MinuteNow < 15 is
reached launch the second one w 10min itervals and stop the first one.

hope it helps

Regards - OHM# (e-mail address removed)
 
C

Crirus

Well, I dont think I need two timers...
If I can start the timer in order to raise tick event exact as if it was
started at base start time (minute 15) the problem is solved without any
boolean or second timer

All I need is to figure out how many minutes I still need to wait until
first tick

So if I enable the timer at minute 33 of curent hour, I need to set the
interval to 2 minutes in order to haev first tick at minute 35

If I enable the timer on minute 10, I have to wait 5 minute untill first
ticck should occur, on minute 15..and so one

All I need is that interval that is first wait, considering hte interval and
base tick minute (e.g. 15)
 
A

Armin Zingler

Crirus said:
This is more a logical problem than a VB.

I have this situation:

A timer that need to tick at each 10 minutes starting on minute 15 of
curent hour.

But I want to calculate the next tick time whenever I want to start
the timer.

So, I have ticks on minutes 25, 35, 45, 55, 05

If I start the timer on minute 33 I need that 2 minutes interval
untill 35 in order to start the timer..
On tick event I reset the interval to 10 minutes.
How to calculate that 2 minute interval?


It does not calculate the 2 minutes, but it should work:

Private Sub StartTimer()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Function IsAlarmMinute(ByVal Minute As Integer) As Boolean
Return ((Minute + 5) Mod 10) = 0
End Function

Private Sub Timer1_Tick( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Timer1.Tick

If IsAlarmMinute(DateTime.Now.Minute) Then
Debug.WriteLine("feed me!")
End If
End Sub
 
O

One Handed Man [ OHM# ]

You could use this logic. . . .



Regards - OHM#



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim minute As Int32

Dim interval As Int32



For minute = 0 To 60

Select Case minute

Case 0 To 4

interval = 5 - minute

Case 6 To 14

interval = 15 - minute

Case 16 To 24

interval = 25 - minute

Case 26 To 34

interval = 35 - minute

Case 36 To 44

interval = 45 - minute

Case 46 To 54

interval = 55 - minute

Case 56 To 60

interval = 5 + (60 - minute)

Case 5, 15, 25, 35, 45, 55

interval = 10

End Select

Debug.WriteLine("Minute: " & minute & " - Interval = " & interval)

Next

End Sub

Prints This Out . . .



Minute: 0 - Interval = 5

Minute: 1 - Interval = 4

Minute: 2 - Interval = 3

Minute: 3 - Interval = 2

Minute: 4 - Interval = 1

Minute: 5 - Interval = 10

Minute: 6 - Interval = 9

Minute: 7 - Interval = 8

Minute: 8 - Interval = 7

Minute: 9 - Interval = 6

Minute: 10 - Interval = 5

Minute: 11 - Interval = 4

Minute: 12 - Interval = 3

Minute: 13 - Interval = 2

Minute: 14 - Interval = 1

Minute: 15 - Interval = 10

Minute: 16 - Interval = 9

Minute: 17 - Interval = 8

Minute: 18 - Interval = 7

Minute: 19 - Interval = 6

Minute: 20 - Interval = 5

Minute: 21 - Interval = 4

Minute: 22 - Interval = 3

Minute: 23 - Interval = 2

Minute: 24 - Interval = 1

Minute: 25 - Interval = 10

Minute: 26 - Interval = 9

Minute: 27 - Interval = 8

Minute: 28 - Interval = 7

Minute: 29 - Interval = 6

Minute: 30 - Interval = 5

Minute: 31 - Interval = 4

Minute: 32 - Interval = 3

Minute: 33 - Interval = 2

Minute: 34 - Interval = 1

Minute: 35 - Interval = 10

Minute: 36 - Interval = 9

Minute: 37 - Interval = 8

Minute: 38 - Interval = 7

Minute: 39 - Interval = 6

Minute: 40 - Interval = 5

Minute: 41 - Interval = 4

Minute: 42 - Interval = 3

Minute: 43 - Interval = 2

Minute: 44 - Interval = 1

Minute: 45 - Interval = 10

Minute: 46 - Interval = 9

Minute: 47 - Interval = 8

Minute: 48 - Interval = 7

Minute: 49 - Interval = 6

Minute: 50 - Interval = 5

Minute: 51 - Interval = 4

Minute: 52 - Interval = 3

Minute: 53 - Interval = 2

Minute: 54 - Interval = 1

Minute: 55 - Interval = 10

Minute: 56 - Interval = 9

Minute: 57 - Interval = 8

Minute: 58 - Interval = 7

Minute: 59 - Interval = 6

Minute: 60 - Interval = 5



Well, I dont think I need two timers...
If I can start the timer in order to raise tick event exact as if it
was started at base start time (minute 15) the problem is solved
without any boolean or second timer

All I need is to figure out how many minutes I still need to wait
until first tick

So if I enable the timer at minute 33 of curent hour, I need to set
the interval to 2 minutes in order to haev first tick at minute 35

If I enable the timer on minute 10, I have to wait 5 minute untill
first ticck should occur, on minute 15..and so one

All I need is that interval that is first wait, considering hte
interval and base tick minute (e.g. 15)

Regards - OHM# (e-mail address removed)
 
C

Cor

Hi Crirus,

I should not write this because maybe a lot of comments,
But why not just threading.thread.sleep(600000) for the 10 minutes and the
first time the difference from now till the interval?

Cor
 
O

One Handed Man [ OHM# ]

This is a different approach than the OP wanted. What you have done is
caused a 1Minute tick and determined if it is an alarm minute. However, the
logic does work because I tested it

I guess its up to him.

Regards - OHM#


Armin said:
It does not calculate the 2 minutes, but it should work:

Private Sub StartTimer()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Function IsAlarmMinute(ByVal Minute As Integer) As Boolean
Return ((Minute + 5) Mod 10) = 0
End Function

Private Sub Timer1_Tick( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Timer1.Tick

If IsAlarmMinute(DateTime.Now.Minute) Then
Debug.WriteLine("feed me!")
End If
End Sub

Regards - OHM# (e-mail address removed)
 
C

Crirus

That is too particular
I could have any base start minute and any interval of events occurence...
and of course any enabling minute of an hour
So I really need to calculate what is the first minute after Now.Minute when
the event should occur as it was started at baseStartMinute
 
A

Armin Zingler

Crirus said:
That is too particular
I could have any base start minute and any interval of events
occurence...
and of course any enabling minute of an hour
So I really need to calculate what is the first minute after
Now.Minute when the event should occur as it was started at
baseStartMinute

Of course you can replace the constants 5 and 10 by variables. The code also
works with every start minute, so does the code really not work the way you
need it?
 
A

Armin Zingler

One Handed Man said:
This is a different approach than the OP wanted.
Why?

What you have done
is caused a 1Minute tick

1 second tick. :)
and determined if it is an alarm minute.

yes, but why not? Waiting for a certain minute value can only be done by a)
using a timer checking the minute e.g. each second, or b) running in a loop
and check the minute value.
However, the logic does work because I tested it

I guess its up to him.

Yes
 
C

Crirus

I'm writing this right now (not testet):

Private TickInterval as Integer=10

Private Sub StartTimer(ByVal BaseTickMinute As Integer, ByVal Interval As
Integer)
myTimer.Interval=FirstSleep(BaseTickMinute,Interval)
myTimer.Start()
End Sub

'Return first interval for tick event
Private Function FirstSleep(ByVal BaseTickMinute As Integer, ByVal
Interval As Integer) As Integer
Dim nextTickMinute As Integer
Dim thisMinute As Integer = Now.Minute
If thisMinute > BaseTickMinute Then
nextTickMinute = Math.Ceiling((thisMinute - BaseTickMinute) /
Interval) * Interval
Return nextTickMinute - thisMinute
Else
nextTickMinute = Math.Ceiling((60 - BaseTickMinute + thisMinute)
/ Interval) * Interval
Return nextTickMinute - thisMinute
End If
End Function

Private Sub GameTick_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
GameTick.Interval = tickInterval * 60000
' Code here
End Sub

I will test later to see if it work
 
P

Phill. W

.. . .
A timer that need to tick at each 10 minutes starting on minute 15
of curent hour. .. . .
If I start the timer on minute 33 I need that 2 minutes interval untill 35
in order to start the timer..
How to calculate that 2 minute interval?

Over-simple Solution: Don't bother!

Timer's are realtively light-weight. It's what the likes of you and I
/do/ with them that kills machines ;-)

Use a Timer that fires once /every minute/ and check the current
minute each time around. If it matches your criteria, do whatever
it is that needs doing.

Sub tmrPoll_Elapsed( ...
tmrPoll.Enabled = False ' Just habit...

Select Case System.DateTime.Now().Minute
Case 5, 15, 25, 35, 45, 55
DoSomethingUseful()
End Select

tmrPoll.Enabled = True
End Sub

If you want to make this smart enough so that it /doesn't/ start up
until ??:15 if you launch the program at, say, ??:02, that's up to you.

HTH,
Phill W.
 
C

Chris Dunaway

You could use this logic. . . .



Regards - OHM#



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim minute As Int32

Dim interval As Int32



For minute = 0 To 60

Select Case minute

Case 0 To 4

interval = 5 - minute

Case 6 To 14

interval = 15 - minute

Case 16 To 24

interval = 25 - minute

Case 26 To 34

interval = 35 - minute

Case 36 To 44

interval = 45 - minute

Case 46 To 54

interval = 55 - minute

Case 56 To 60

interval = 5 + (60 - minute)

Case 5, 15, 25, 35, 45, 55

interval = 10

End Select

Debug.WriteLine("Minute: " & minute & " - Interval = " & interval)

Next

End Sub

Prints This Out . . .



Minute: 0 - Interval = 5

Minute: 1 - Interval = 4

Minute: 2 - Interval = 3

Minute: 3 - Interval = 2

Minute: 4 - Interval = 1

Minute: 5 - Interval = 10

Minute: 6 - Interval = 9

Minute: 7 - Interval = 8

Minute: 8 - Interval = 7

Minute: 9 - Interval = 6

Minute: 10 - Interval = 5

Minute: 11 - Interval = 4

Minute: 12 - Interval = 3

Minute: 13 - Interval = 2

Minute: 14 - Interval = 1

Minute: 15 - Interval = 10

Minute: 16 - Interval = 9

Minute: 17 - Interval = 8

Minute: 18 - Interval = 7

Minute: 19 - Interval = 6

Minute: 20 - Interval = 5

Minute: 21 - Interval = 4

Minute: 22 - Interval = 3

Minute: 23 - Interval = 2

Minute: 24 - Interval = 1

Minute: 25 - Interval = 10

Minute: 26 - Interval = 9

Minute: 27 - Interval = 8

Minute: 28 - Interval = 7

Minute: 29 - Interval = 6

Minute: 30 - Interval = 5

Minute: 31 - Interval = 4

Minute: 32 - Interval = 3

Minute: 33 - Interval = 2

Minute: 34 - Interval = 1

Minute: 35 - Interval = 10

Minute: 36 - Interval = 9

Minute: 37 - Interval = 8

Minute: 38 - Interval = 7

Minute: 39 - Interval = 6

Minute: 40 - Interval = 5

Minute: 41 - Interval = 4

Minute: 42 - Interval = 3

Minute: 43 - Interval = 2

Minute: 44 - Interval = 1

Minute: 45 - Interval = 10

Minute: 46 - Interval = 9

Minute: 47 - Interval = 8

Minute: 48 - Interval = 7

Minute: 49 - Interval = 6

Minute: 50 - Interval = 5

Minute: 51 - Interval = 4

Minute: 52 - Interval = 3

Minute: 53 - Interval = 2

Minute: 54 - Interval = 1

Minute: 55 - Interval = 10

Minute: 56 - Interval = 9

Minute: 57 - Interval = 8

Minute: 58 - Interval = 7

Minute: 59 - Interval = 6

Minute: 60 - Interval = 5





Regards - OHM# (e-mail address removed)

May I offer this solution:

Private Function GetInterval(ByVal min As Integer) As Integer
Dim iNext As Integer
iNext = ((min \ 10) * 10) + 5
If (min Mod 10) > 5 Then iNext += 10

If (iNext - min) = 0 Then
Return 10
Else
Return (iNext - min)
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top