Posted by: Rhonda | November 6, 2009

Timers in .NET

I was researching timers the other day and realized that there are three variations in the .NET Framework.

stopwatch

The three types of timers are explained below.

1 – System.Timers.Timer

The System.Timers.Timer class timer is considered a server-based timer that was designed and optimized for use in multithreaded environments. It can be accessed safely from multiple threads.

Source Code

csharp2 

Output

image

2 – System.Threading.Timer

The System.Threading.Timer class timer uses a TimerCallBack Delegate to specify the associated methods. The methods do not execute in the thread that created the timer; they execute in a separate thread that is automatically allocated by the system.

Source Code

csharp2 

Output

image

3 – Windows.Forms.Timer

The Windows.Forms.Timer class works synchronously with the Windows Form, so that it will not interrupt any operations. It initializes on the UI thread.

Source Code

csharp2 

Output

image  image  image

There are three choices for timers in .NET.  It just depends on what you want to do.

More Information

Until next time…


Responses

  1. [...] Timers in .NET (Rhonda Tipton) [...]

  2. hhhmmm I initially read your blog post cause I was looking for the differences in why/when to use the different timers. Unfortunately just as I get to the “…depends on what you want to do” you end the post. DOH! Can you expand on this please?

  3. @phenry9999 – I meant this to be a complete beginner post. I am researching the why/when and was planning on doing another post…hopefully in the near future. There is not a ton of info on the differences.

  4. Thanks for the response. Very cool, I subscribed to your rss feed, can’t wait for the update. Thanks for looking into this (I hope :>HAHA). Have a good day!


Leave a response

You must be logged in to post a comment.

Categories