秒表(系统诊断)和系统定时器

发布于 2024-08-07 03:17:07 字数 106 浏览 3 评论 0原文

我是处理线程的新手。

System.Diagnostics 和 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十秒萌定你 2024-08-14 03:17:07

秒表用于测量时间间隔。计时器用于安排方法在将来的某个时刻执行。他们是完全不同的。

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

迷迭香的记忆 2024-08-14 03:17:07

System.Diagnostics 命名空间提供了允许您进行交互的类包含系统进程、事件日志和性能计数器。

System.Timers 命名空间提供计时器组件,允许您按指定的时间间隔引发事件。

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.

伴随着你 2024-08-14 03:17:07

使用 System.Timers 时要小心。

.Net 中基本上有三个计时器

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

.Net 中提供三个计时器

需要注意的关键是收集问题。

如果您的线程在某个点之后没有引用某个对象,则该对象可能会被垃圾收集。这实际上是 IDisposable 模式的关键原因之一,因为调用 dispose 意味着让对象保持活动状态,至少直到 dispose() 调用结束。这是当你说时调用的方法

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

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.
仙女山的月亮 2024-08-14 03:17:07

作为另一个选择,我一直在使用 我的应用程序中的 BackgroundWorker 类 效果良好

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文