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

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

哪一个最好?


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

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

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

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

在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进行线程安全执行

我从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.

这两个类在功能上是等效的,除了System.Timers.Timer有一个选项可以通过设置SynchronizingObject来调用ISynchronizeInvoke来调用所有的计时器过期回调。否则,两个计时器都调用线程池线程的过期回调。

当你将System.Timers.Timer拖到Windows窗体设计面上时,Visual Studio将SynchronizingObject设置为窗体对象,这将导致在UI线程上调用所有过期回调。

System.Threading.Timer是一个简单、轻量级的定时器,使用回调方法,由线程池线程提供服务。不建议与Windows窗体一起使用,因为它的回调不会发生在用户界面线程上。System.Windows.Forms.Timer是Windows窗体的更好选择。对于基于服务器的定时器功能,可以考虑使用System.Timers。定时器,它引发事件并具有其他特性。

上面没有提到的一个重要区别是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);

微软对此的信息(见MSDN备注):

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 (.NET Framework only), 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; it executes on the UI thread. System.Web.UI.Timer (.NET Framework only), an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.

值得一提的是,System.Timers.Timer在。net Core 1.0中已弃用,但在。net Core 2.0 (/ . net Standard 2.0)中再次实现。 . net标准2.0的目标是尽可能容易地从。net框架中切换出来,这可能是它回归的原因。

当它被弃用时,. net可移植性分析器Visual Studio插件建议使用System.Threading.Timer代替。

看起来微软更喜欢System.Threading.Timer而不是System.Timers.Timer。

编辑说明2018-11-15: 我不得不改变我的答案,因为关于。net Core 1.0的旧信息不再有效了。

正如其他人提到的MS Docs链接,System.Timers.Timer和System.Threading.Timer之间的一个主要区别是System.Threading.Timer执行一个定义一次的回调方法,而System.Timers.Timer对事件做出反应,因此支持多个订阅者,也可以删除。

如上所述,System.Timers.Timer在内部使用System.Threading.Timer,例如,Enable=false处理内部计时器,并在Enable=true / Start()上重新创建它: https://source.dot.net/#System.ComponentModel.TypeConverter/System/Timers/Timer.cs