我最近一直在检查一些可能的定时器,System.Threading.Timer和System.Timers.Timer对我来说是必要的(因为它们支持线程池)。
我正在制作一款游戏,我计划使用各种类型的事件,以及不同的间隔等等。
哪一个最好?
我最近一直在检查一些可能的定时器,System.Threading.Timer和System.Timers.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.
其他回答
正如其他人提到的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的旧信息不再有效了。
上面没有提到的一个重要区别是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);
System.Threading.Timer是一个简单、轻量级的定时器,使用回调方法,由线程池线程提供服务。不建议与Windows窗体一起使用,因为它的回调不会发生在用户界面线程上。System.Windows.Forms.Timer是Windows窗体的更好选择。对于基于服务器的定时器功能,可以考虑使用System.Timers。定时器,它引发事件并具有其他特性。
源
这两个类在功能上是等效的,除了System.Timers.Timer有一个选项可以通过设置SynchronizingObject来调用ISynchronizeInvoke来调用所有的计时器过期回调。否则,两个计时器都调用线程池线程的过期回调。
当你将System.Timers.Timer拖到Windows窗体设计面上时,Visual Studio将SynchronizingObject设置为窗体对象,这将导致在UI线程上调用所有过期回调。