VB.NET Timer and Thread question

V

Vincent

Hi,

I was trying to do something in a Thread that starts by a
timer. I turn off the timer at the begining of the thread
incase it fires again when the thread is still running. Then
turn on the timer again before the thread finishes. To my
surprise, the timer never come alive again once it has been
turned of in the thread. To test the problem I made a simple
Windows Form project, put a Beep() in the timer. It supposed
to hear beep beep beep every one seconds, but can only hear
one. Here enclosed my clode below. Any help or suggestions are
really appreciated.

Regards,
Vincent

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Timer1.Tick
Beep()
Dim thr As New Threading.Thread(AddressOf ProcTest)
thr.Start()
End Sub

Private Sub ProcTest()
Timer1.Enabled = False
'Timer1.Stop()
Timer1.Enabled = True
'Timer1.Start()
End Sub
 
C

Cor Ligthert

Hi Vincent,

There are 3 timers
windows.forms.form.timer
System.timers.timer
System.threading.timer

The one you are using now is as far as I can see the
windows.forms.form.timer and I think not the one the most suitable for your
problem.

Cor
 
J

Jabco

Hi Vincent,

There are 3 timers
windows.forms.form.timer
System.timers.timer
System.threading.timer

The one you are using now is as far as I can see the
windows.forms.form.timer and I think not the one the most suitable for
your problem.

Cor

Thanks for your suggestion. The System.threading.timer works great! Just
curious, I also tried System.timers.timer, that works as well.

Don't understand why the System.Windows.Forms.Timer has the strange
problem. Maybe it is by design, but why vb.net doesn't give a compile error
or warning something. I wonder if it does the same thing in C#.
 
A

Armin Zingler

Vincent said:
Hi,

I was trying to do something in a Thread that starts by a
timer. I turn off the timer at the begining of the thread
incase it fires again when the thread is still running. Then
turn on the timer again before the thread finishes. To my
surprise, the timer never come alive again once it has been
turned of in the thread. To test the problem I made a simple
Windows Form project, put a Beep() in the timer. It supposed
to hear beep beep beep every one seconds, but can only hear
one. Here enclosed my clode below. Any help or suggestions are
really appreciated.

Regards,
Vincent

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Timer1.Tick
Beep()
Dim thr As New Threading.Thread(AddressOf ProcTest)
thr.Start()
End Sub

Private Sub ProcTest()
Timer1.Enabled = False
'Timer1.Stop()
Timer1.Enabled = True
'Timer1.Start()
End Sub

You must access the timer from the thread that created the timer.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
S

Sobhan Vezzu

Hi Cor,
Whenever time is elapsed it puts an event in the message queue and
windows will work on one message after one. Suppose you have set timer
interval at 200ms there is not guarantee that it will come to ur
Timertick event at 200 ms

This runs on the forms thread and also has the accuracy of time so
timertick event will happen after 200 ms
This runs on a different thread but has the same functionlity as
systems.timer.time so we need to be careful while updating the form
variables as windows forms are not thread safe.

Regards
Sobhan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top