Re: Timer fires inconsistantely



Looks like I solved my own problem, here's the solution the benifit of
others:
This like of code "AddHandler t.Elapsed, AddressOf TimerFired" should only
be triggered ONCE and only once during the entire execution of the program.
To prevent it from triggering the second time, I simply declared a boolean
variable to be true and then once the button has been triggered once, it
resets the boolean value to false, so it will not recall that line of code,
the handler the second time.




"derSchweiz" <keine@xxxxxxxx> schrieb im Newsbeitrag
news:KtV%f.18730$7a.7369@xxxxxxxxxxx
I have an application that requires the use of a timer and I have created
the timer object globally:

Private t As New System.Timers.Timer(4000)

And I have a button that that triggers an event handler:


Private Sub myButton(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
t.Enabled = True
AddHandler t.Elapsed, AddressOf TimerFired
End Sub

Which triggers:

Private Sub TimerFired(ByVal sender As Object, _ByVal e As
System.Timers.ElapsedEventArgs)
MsgBox(commandID)
commandID = commandID + 1

If commandID = 7 Then
t.Enabled = False
t.Stop()
t.Close()
commandID = 0
isFirstStart = False
End If
End Sub

The first the time button gets clicked, the TimerFired method fires every
4 seconds, it works great the first time, however on the second time it
goes like
Fire Fire... 4 seconds.. Fire fire... 4 seconds... fire fire

On the third time it will go like:

fire fire fire... 4 seconds fire fire fire...


How can I make it so that it will go like

fire... 4 seconds... fire... 4 seconds... fire 4 seconds etc...

each time I press the button?

p.s Please explain as simply as possible, this is my first time
programming in VB.

Thanks for any input





.