473,326 Members | 2,126 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,326 software developers and data experts.

VB.NET Timer and Thread question

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
Nov 20 '05 #1
10 1454
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
Nov 20 '05 #2
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
Nov 20 '05 #3
"Cor Ligthert" <no**********@planet.nl> wrote in
news:et**************@TK2MSFTNGP10.phx.gbl:
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#.
Nov 20 '05 #4
"Cor Ligthert" <no**********@planet.nl> wrote in
news:et**************@TK2MSFTNGP10.phx.gbl:
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#.
Nov 20 '05 #5
"Vincent" <Ja***@serbe.com> schrieb
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

Nov 20 '05 #6
"Vincent" <Ja***@serbe.com> schrieb
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

Nov 20 '05 #7
Hi Cor,
windows.forms.form.timer 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

System.timers.timer This runs on the forms thread and also has the accuracy of time so
timertick event will happen after 200 ms
System.threading.timer
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

Jabco <Ja***@serbe.com> wrote in message news:<Xn**************************@207.46.248.16>. .. "Cor Ligthert" <no**********@planet.nl> wrote in
news:et**************@TK2MSFTNGP10.phx.gbl:
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#.

Nov 20 '05 #8
Hi Cor,
windows.forms.form.timer 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

System.timers.timer This runs on the forms thread and also has the accuracy of time so
timertick event will happen after 200 ms
System.threading.timer
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

Jabco <Ja***@serbe.com> wrote in message news:<Xn**************************@207.46.248.16>. .. "Cor Ligthert" <no**********@planet.nl> wrote in
news:et**************@TK2MSFTNGP10.phx.gbl:
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#.

Nov 20 '05 #9
Vincent,
In addition to the others comments, the following recent articles in MSDN
Magazine explain the difference between the three thread objects in .NET &
when to use each.

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx

Hope this helps
Jay

And yes, C# will exhibit the same basic behavior.

Hope this helps
Jay

"Vincent" <Ja***@serbe.com> wrote in message
news:Xn**************************@207.46.248.16...
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

Nov 20 '05 #10
Vincent,
In addition to the others comments, the following recent articles in MSDN
Magazine explain the difference between the three thread objects in .NET &
when to use each.

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx

Hope this helps
Jay

And yes, C# will exhibit the same basic behavior.

Hope this helps
Jay

"Vincent" <Ja***@serbe.com> wrote in message
news:Xn**************************@207.46.248.16...
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

Nov 20 '05 #11

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

Similar topics

3
by: brian | last post by:
I have an ASP.Net application that uses impersonation. This works fine for accessing/executing the application. However, the app utilizes a timer, that when fired uses the <machine>ASPNET...
5
by: Richard P | last post by:
I need some help on timers. My app is asp.net 1.1 website running in a shared hosting environment with a third-party service provider. I currently request and cache 20 - 40 remote RSS feeds. When a...
1
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer...
2
by: steve | last post by:
Since System.Threading.Timer uses the threadpool to do its stuff what happens when (a) You try to create a timer and the thread pool is *exhausted* (b) The timer is due to fire AND all threads...
10
by: Vincent | last post by:
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...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
4
by: Ben | last post by:
Hello everybody I got confused by this problem for which I don't have a logical explanation. There is a Thread (ThreadA) which receives Events from another system thread (ThreadS). ThreadA then...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
5
by: John A. Bailo | last post by:
From a Windows service (NET 2.0) I want to launch serveral threads in a for loop that invokes a method using: new Thread(delegate() { myMethod(248);}).Start(); Will those threads stay...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.