473,382 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Timer1_Elapsed problem.

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
6 4556
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

<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
<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.