views:

210

answers:

4

I am new to handling threads.

What is the role of System.Diagnostics and System.Timers in the context of Threading ? Both are alternative to each other or they implemented for doing some unique tasks?

+7  A: 

StopWatch is for measuring time intervals. Timers are for scheduling methods to execute at some point in the future. They are completely different.

spender
Thanks for the clarification. :)
+3  A: 

The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters.

The System.Timers namespace provides the Timer component, which allows you to raise an event on a specified interval.

Mitch Wheat
Thank you very much Mitch.
A: 

Be vary careful with System.Timers.

There are basically three timers in .Net

System.Timers.Timer
System.Threading.Timer
System.Windows.Forms.Timer

Three Timers available in .Net

The key thing to watch out for is the collection problem.

If your thread makes no reference to an object after a certain point, it can be garbage collected. This is actually one of the key reasons for the IDisposable pattern, because calling dispose means you keep the object alive until AT LEAST the end of the dispose() call. This is the method called when you say

using(var myobj = new System.Threading.Timers()) 
{
    //run program here 
} //Timer can be collected from now.
Spence
A: 

As another option I've been using the BackgroundWorker class in my applications with good results

armannvg