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

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

哪一个最好?


当前回答

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

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

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

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

其他回答

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

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

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

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

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

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

正如其他人提到的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

微软对此的信息(见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的旧信息不再有效了。