473,385 Members | 1,848 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,385 software developers and data experts.

Timer fires inconsistantely

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

Apr 14 '06 #1
5 1069
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" <ke***@keine.de> schrieb im Newsbeitrag
news:KtV%f.18730$7a.7369@pd7tw1no...
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

Apr 15 '06 #2
derSchweiz,

Any reason that you don't use the Forms timer? In my expririence does that
with a normal form application give much less problems than the system and
threading timers.

Cor
"derSchweiz" <ke***@keine.de> schreef in bericht
news:KtV%f.18730$7a.7369@pd7tw1no...
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

Apr 15 '06 #3
Hi derSchweiz,

Your solution to the problem works, but wouldn't it have been simpler
to wire up the event to the handler outside the Button_Click event ?
Say, in your Sub New() ?

Regards,

Cerebrus.

Apr 15 '06 #4
Unless you have a specific reason to block the timer from firing, create the
timer "WithEvents" and then use the IDE to create the timer_elapsed handler.
Then you can avoid the AddHandler statement entirely. In the timer
definition, set Enabled =False. Then in your Button handler, simply set the
timer's Enabled property to true.

Mike Ober.

"Cerebrus" <zo*****@sify.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi derSchweiz,

Your solution to the problem works, but wouldn't it have been simpler
to wire up the event to the handler outside the Button_Click event ?
Say, in your Sub New() ?

Regards,

Cerebrus.


Apr 15 '06 #5

My program might not be the most efficient but im just programming for fun
as a hobby, making homebrew applications.

Thanks again for everyone's input!

"Cor Ligthert [MVP]" <no************@planet.nl> schrieb im Newsbeitrag
news:OV****************@TK2MSFTNGP02.phx.gbl...
derSchweiz,

Any reason that you don't use the Forms timer? In my expririence does that
with a normal form application give much less problems than the system and
threading timers.

Cor
"derSchweiz" <ke***@keine.de> schreef in bericht
news:KtV%f.18730$7a.7369@pd7tw1no...
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


Apr 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mr. B | last post by:
My current app has a timer that I kick ON in my Form1_Load as follows: ' Set Up the Timer Function Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second t.Enabled = True ' False to Turn OFF...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
6
by: whtinkm | last post by:
Hi, All Recently, my project need some code like following: using System; using System.Threading; namespace MyTimerTest { class Class1 {
6
by: Antti Laakso | last post by:
Hi i have function like above Public Sub halytystutkinta() Dim ds As New DataSet ds = dl2.HaeHalytys() Dim onkohal As Int16 onkohal = ds.Tables(0).Rows(0).Item("onkohalytys") halid =...
4
by: Brian P | last post by:
I have a service that is driven by a timer that fires every 5 seconds. For the most part, it works fine. But every once in a while the timer fires twice. In the log I can see that when it fires...
5
by: Flack | last post by:
Hey guys, Here is what I am trying to achieve: I have a grid, and every once in a while the grid will receive a message to add a new row and highlight it (change the backcolor) for five...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
4
by: Boki | last post by:
Hi All, I have a timer, if my data queue Q has data, the timer should start work, if there is no data in Q, the timer should stop. However, there is an event can fire timer to start. Should I...
2
by: Johnny Jörgensen | last post by:
I've got a process I want to run in a thread separate from my main application thread, so I've used a backgroundworker component, and in frmMain.Load I invoke the code using...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.