StopWatch (System.Diagnostics) and System.Timers

I am new to thread handling.

What is the role of System.Diagnostics and System.Timers in the context of Threading? Are both alternatives to each other or are they implemented to perform some unique task?

+2


source to share


4 answers


StopWatch is designed to measure time intervals. Timers are meant to allow scheduling techniques to run at some point in the future. They are completely different.



+11


source


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



The System.Timers namespace provides a Timer component that allows you to raise an event by a specified interval.

+3


source


Be careful with System.Timers.

In .Net

there are three timers.
System.Timers.Timer
System.Threading.Timer
System.Windows.Forms.Timer

      

Three timers are available in .Net

The key point to look out for is the collection issue.

If your thread does not refer to an object after a certain point, it might get garbage collected. This is actually one of the key reasons for the IDisposable pattern, because calling dispose means that you keep the object until you reach 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.

      

+1


source


As another option, I have used the BackgroundWorker class in my applications with good results

0


source







All Articles