<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