开发者

StopWatch (System.Diagnostics) and System.Timers

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?


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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜