By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Best way to stop and restart a System.Timers.Timer

P: n/a
Hi,

What is the best way to release all resources holded by the Timer
(myTimer from class System.Timers.Timer)?

Is it:
1- myTimer.dispose
2- myTimer.enabled = false
3- myTimer.close

I want to have the timer set to nothing so I can recreate a new one, I
want to do this in a second step:

myTimer = New System.Timers.Timer
AddHandler myTimer.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

I am asking this because it seem that the timer loop a couple of time
before being stopped, and raising errors by the way.
This is my original code:
myTimer.Dispose()
myTimer = New System.Timers.Timer
AddHandler tmrClientQwery.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

Thank you very much

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The Timer.Enabled actually destroys the timer internally anyway. The extra
timer event's you're seeing are probably due to messages that are in the
queue. This can happen if you do a lot of processing in the timer tick
handler such as drawing on screen or disc access.

A more accurate and reliable timer can be found in System.Threading. The
dlegate for this timer is called directly and there are never any messages
hanging around in the queue.

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"User" <gu***@guest.com> wrote in message
news:x3dHc.9484$Rf.5781@edtnps84...
Hi,

What is the best way to release all resources holded by the Timer
(myTimer from class System.Timers.Timer)?

Is it:
1- myTimer.dispose
2- myTimer.enabled = false
3- myTimer.close

I want to have the timer set to nothing so I can recreate a new one, I
want to do this in a second step:

myTimer = New System.Timers.Timer
AddHandler myTimer.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

I am asking this because it seem that the timer loop a couple of time
before being stopped, and raising errors by the way.
This is my original code:
myTimer.Dispose()
myTimer = New System.Timers.Timer
AddHandler tmrClientQwery.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

Thank you very much

Nov 20 '05 #2

P: n/a
Thanks ,

I tried this System.Threading.Timer. Is it possible that it
automatically create many threads that run the code in the elapsed event?

How can I be sure that I have only one thread under this timer?

Thank you!

Bob Powell [MVP] wrote:
The Timer.Enabled actually destroys the timer internally anyway. The extra
timer event's you're seeing are probably due to messages that are in the
queue. This can happen if you do a lot of processing in the timer tick
handler such as drawing on screen or disc access.

A more accurate and reliable timer can be found in System.Threading. The
dlegate for this timer is called directly and there are never any messages
hanging around in the queue.


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.