PC Review


Reply
Thread Tools Rate Thread

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

 
 
User
Guest
Posts: n/a
 
      8th Jul 2004
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

 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      8th Jul 2004
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" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
User
Guest
Posts: n/a
 
      9th Jul 2004
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.
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of System.Timers.timer - Assigning an object to timer Gina_Marano Microsoft C# .NET 2 8th Mar 2007 12:51 AM
System.Timers.Timer - use Start/Stop or Enabled? Tom Microsoft VB .NET 3 8th Aug 2005 06:51 PM
System.Timers.Timer/System.Threading.Timer Not Firing At All on Dev Server james.e.coleman@gmail.com Microsoft ASP .NET 1 22nd Feb 2005 09:41 PM
RE: Timers not fireing System.Windows.Forms.Timer and System.Timers.Ti =?Utf-8?B?UmljaE0=?= Microsoft Dot NET Framework Forms 3 19th Aug 2004 10:35 AM
RE: Timers not fireing System.Windows.Forms.Timer and System.Timer =?Utf-8?B?VGlt?= Microsoft Dot NET Framework Forms 0 18th Aug 2004 11:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:00 AM.