473,385 Members | 1,400 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.

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 27626
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...
3
by: bob | last post by:
Could someone tell me how to create a Timer thread that calls a function 60 times a second in VC++? Thanks.
4
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the...
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...
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...
0
by: mavrik | last post by:
Hi all, I have an application written in C, which runs on Sun OS platform. I had to port this application from Sun OS to Linux AS. My whoe application works fine, but it gives some problem in...
1
by: Michael Howes | last post by:
I and trying to detect idle time or sorts in the applications I'm building. To do this the main Form also inherits from IMessageFilter and I listen for WM_MOUSEMOVE. When that happens I reset a...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.