|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Timer1_Elapsed problem.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?? Just put:
'Exit Sub' after the MsgBox statement or have a boolean value that changes when you process your select statement. 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 Crouchie,
? VB.NET does not have fall through case statements that C & C++ does. Hope this helps Jay Show quoteHide quote "Crouchie1998" <crouchie1***@discussions.microsoft.com> wrote in message news:uCD9u2tMFHA.1476@TK2MSFTNGP09.phx.gbl... > Just put: > > 'Exit Sub' after the MsgBox statement or have a boolean value that changes > when you process your select statement. > > <richardkre***@northwesternmutual.com> wrote in message
Show quoteHide quote news:1111934882.375718.86360@o13g2000cwo.googlegroups.com... First, I would recommend not using strings.> 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?? > 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 <richardkre***@northwesternmutual.com> schrieb:
Show quoteHide quote >I am using the code below to notify the user to do different tasks at Use a larger timer interval (500 ms, for example).> 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. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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/microsoft.public.dotnet.languages.vb/msg/835f79ef3acde155 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 <richardkre***@northwesternmutual.com> wrote in message Show quoteHide quote news:1111934882.375718.86360@o13g2000cwo.googlegroups.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?? > Richard,
You have so many answers that I did not look at your problem in depth, however, the first thing I do in a Timer event is setting the timer enabled to false. The last thing I do in a timer event when it has to be used again is setting enabled to true. Maybe it helps, Cor
Locate MessageBox over Window
Error with Custom Control Closing form... what if the database connection isn't close MaskedEdit Control Friend WithEvents Upload file programmatically DataGrid OnPaint/? -How Do I Draw A GDI Type Circle On Top Of A DataGrid add-in missing from toolbar. FileSystemWatcher: Fix for lowercase? |
|||||||||||||||||||||||