By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Timer1_Elapsed problem.

P: n/a
I am using the code below to notify the user to do different tasks at
certain times of the day:

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Select Case Now().ToString("hh:mm:ss")
Case "08:30:00"
MsgBox("Do this")
Case "12:30:00"
MsgBox("Do that")
End Select
End Sub

The problem I'm having is that the messagebox popups ten times. I guess
for the each tenth of a second.
If I use: Now().ToString("hh:mm:ss:fff") for milliseconds and change
the 'case' statement to "08:30:00:000" it doesn't popup at all.

I just need it to popup one time.
Any ideas??

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Just put:

'Exit Sub' after the MsgBox statement or have a boolean value that changes
when you process your select statement.
Nov 21 '05 #2

P: n/a

<ri***********@northwesternmutual.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I am using the code below to notify the user to do different tasks at
certain times of the day:

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Select Case Now().ToString("hh:mm:ss")
Case "08:30:00"
MsgBox("Do this")
Case "12:30:00"
MsgBox("Do that")
End Select
End Sub

The problem I'm having is that the messagebox popups ten times. I guess
for the each tenth of a second.
If I use: Now().ToString("hh:mm:ss:fff") for milliseconds and change
the 'case' statement to "08:30:00:000" it doesn't popup at all.

I just need it to popup one time.
Any ideas??


First, I would recommend not using strings.
Set your "alarm" times as DateTimes, then do a compare that way.
You might also want to set some sort of flag to indicate that the event has
been handled and then set a new trigger.
Also, attempting to test for any "particular" point in time exactly will
never work right.
This is due to behaviour kind of like you saw the first time.
With this sort of timer, you are not guaranteed to get the event right on
time.
This leads to the following problem.
Real time event happens. However, your system is busy for a bit.
Finally, the Timer can raise it's event.
However, the "real" time is later than when the event happened. Which can be
from less than a millisecond to a very long time.
Without setting a flag that the desired event has been handled, you can end
up reprocessing the same thing. Like the first behaviour.
So, you tighten your loop. However, now it is very possible that from the
time the event is issued and is actually passed to your event handler, it is
now "later" than your "alarm" test. So now the event never gets handled.
It is like you as a human want to do something at exactly 8:30:00, but if it
is too early or too late, you can't do it. You need to keep looking at the
clock, but you also have other things to do. You will constantly miss the
exact time down to the second.

What you should think about doing is changing the logic to not use exact
compares, for example:
If it is exactly 8:30:00 or Later, then if I haven't raised the alarm then
do it.
When I raise the alarm, set a flag so I know next time that I have already
dealt with this.
If you want it to be recurring, then you can set up a new trigger time.
Ok, I handled it today, so set my next trigger time to the same time
tomorrow.
Or, if I have handled this event, I will wait until a minute after the alarm
time, then reset the handled flag.

I know that all might seem a little confusing, but it comes down to the
logic of it all.

Gerald
Nov 21 '05 #3

P: n/a
<ri***********@northwesternmutual.com> schrieb:
I am using the code below to notify the user to do different tasks at
certain times of the day:

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Select Case Now().ToString("hh:mm:ss")
Case "08:30:00"
MsgBox("Do this")
Case "12:30:00"
MsgBox("Do that")
End Select
End Sub

The problem I'm having is that the messagebox popups ten times. I guess
for the each tenth of a second.

Use a larger timer interval (500 ms, for example).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4

P: n/a
I tried it both ways as you suggested with the: Exit Sub and a boolean
value code below:

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

Select Case Now().ToString("hh:mm:ss")
Case "09:19:00"
If Time_Check = False Then
MsgBox("It's 7:54")
Time_Check = True
End If
Case "09:20:00"
If Time_Check = False Then
MsgBox("It's 7:55")
Time_Check = True
End If
End Select

End Sub

Nov 21 '05 #5

P: n/a
Crouchie,
?
VB.NET does not have fall through case statements that C & C++ does.

Hope this helps
Jay

"Crouchie1998" <cr**********@discussions.microsoft.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Just put:

'Exit Sub' after the MsgBox statement or have a boolean value that changes
when you process your select statement.

Nov 21 '05 #6

P: n/a
Richard,
In addition to the other comments.

I would set the timer interval to the largest granularity I could, which is
normally once a minute.

Rather then compare strings I would compare to DateTime or TimeSpan objects.

Something like:

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' set the Timer.Interval to once a minute
' (let the TimeSpan class do all the funky math for me)
Timer1.Interval = CInt(TimeSpan.FromMinutes(1).TotalMilliseconds)
End Sub
Private ReadOnly EightThirty As TimeSpan = New TimeSpan(8, 30, 0)
Private ReadOnly TwelveThirty As TimeSpan = New TimeSpan(12, 30, 0)

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim currentTime As TimeSpan = DateTime.Now.TimeOfDay
If currentTime.CompareTo(EightThirty) = 0 Then

ElseIf currentTime.CompareTo(TwelveThirty) = 0 Then

End If
End Sub

The "problem" is going to be if the Timer tick misses that one minute.

I would consider using a TimeRange
http://groups-beta.google.com/group/...5f79ef3acde155
to indicate a range the "event" should be active, then set an indicator that
the event was displayed...

Private ReadOnly EightThirty As TimeRange = New TimeRange(#8:30:00 AM#,
#8:45:00 AM#)
Private ReadOnly TwelveThirty As TimeRange = New TimeRange(#12:30:00
PM#, #12:45:00 PM#)

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim currentTime As DateTime = DateTime.Now
If EightThirty.Contains(currentTime) Then

ElseIf TwelveThirty.Contains(currentTime) Then

End If
End Sub

The "problem" with setting an indicator would be to reset it at the start of
each day...

Hope this helps
Jay

<ri***********@northwesternmutual.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I am using the code below to notify the user to do different tasks at
certain times of the day:

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Select Case Now().ToString("hh:mm:ss")
Case "08:30:00"
MsgBox("Do this")
Case "12:30:00"
MsgBox("Do that")
End Select
End Sub

The problem I'm having is that the messagebox popups ten times. I guess
for the each tenth of a second.
If I use: Now().ToString("hh:mm:ss:fff") for milliseconds and change
the 'case' statement to "08:30:00:000" it doesn't popup at all.

I just need it to popup one time.
Any ideas??

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.