我最近一直在检查一些可能的定时器,System.Threading.Timer和System.Timers.Timer对我来说是必要的(因为它们支持线程池)。

我正在制作一款游戏,我计划使用各种类型的事件,以及不同的间隔等等。

哪一个最好?


当前回答

在Jeff Ritcher的书《CLR Via c#》中,他不鼓励使用System.Timers。Timer,这个定时器来源于System.ComponentModel。组件,允许它在Visual Studio的设计面中使用。所以它只在你想在设计平面上设置计时器时才有用。

他更喜欢将System.Threading.Timer用于线程池线程上的后台任务。

其他回答

我从MSDN上找到了一个简短的对比

The .NET Framework Class Library includes four classes named Timer, each of which offers different functionality: System.Timers.Timer, which fires an event and executes the code in one or more event sinks at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime. System.Threading.Timer, which executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime. System.Windows.Forms.Timer, a Windows Forms component that fires an event and executes the code in one or more event sinks at regular intervals. The component has no user interface and is designed for use in a single-threaded environment. System.Web.UI.Timer, an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.

这篇文章给出了一个相当全面的解释:

“比较.NET框架类库中的计时器类”-也可以作为.chm文件获得

具体的区别似乎是System.Timers.Timer是面向多线程应用程序的,因此通过其SynchronizationObject属性是线程安全的,而System.Threading.Timer却不是开箱即用的线程安全的。

我不相信这两者之间有什么区别,因为它涉及到你的间隔可以有多小。

上面没有提到的一个重要区别是System.Timers.Timer会无声地吞咽异常,而System.Threading.Timer不会。

例如:

var timer = new System.Timers.Timer { AutoReset = false };
timer.Elapsed += (sender, args) =>
{
    var z = 0;
    var i = 1 / z;
};
timer.Start();

vs

var timer = new System.Threading.Timer(x =>
{
    var z = 0;
    var i = 1 / z;
}, null, 0, Timeout.Infinite);

在Jeff Ritcher的书《CLR Via c#》中,他不鼓励使用System.Timers。Timer,这个定时器来源于System.ComponentModel。组件,允许它在Visual Studio的设计面中使用。所以它只在你想在设计平面上设置计时器时才有用。

他更喜欢将System.Threading.Timer用于线程池线程上的后台任务。

System.Threading.Timer是一个普通定时器。它在线程池线程(从工作池)上调用您。

timer是一个System.ComponentModel.Component,它包装了System.Threading。定时器,并提供了一些用于在特定线程上调度的附加特性。

取而代之的是,System.Windows.Forms.Timer包装了一个本地消息-only- hwnd,并使用Window Timers在HWNDs消息循环中引发事件。

如果你的应用程序没有UI,而你想要最轻量级和通用的。net定时器,(因为你很高兴自己弄清楚线程/调度)那么System.Threading.Timer是框架中最好的。

我不完全清楚System.Threading.Timer所谓的“不线程安全”问题是什么。也许这和这个问题一样:System.Timers.Timer vs . System.Threading的线程安全性。计时器,也许每个人都只是想说:

使用计时器时,编写竞态条件很容易。请看这个问题: Timer (System.Threading)线程安全 计时器通知的重入,在您完成处理第一个事件之前,计时器事件可以触发并第二次回叫您。例如:使用System.Threading.Timer和Monitor进行线程安全执行