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

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

哪一个最好?


当前回答

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

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

其他回答

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

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

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

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

微软对此的信息(见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

在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.