.net System.Timers.Timer 与 System.Threading.Timer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1416803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Timers.Timer vs System.Threading.Timer
提问by TheAJ
I have been checking out some of the possible timers lately, and System.Threading.Timerand System.Timers.Timerare the ones that look needful to me (since they support thread pooling).
我已经检查出一些可能的计时器最近,和System.Threading.Timer和System.Timers.Timer是看要紧的,我(因为他们支持线程池)中的那些。
I am making a game, and I plan on using all types of events, with different intervals, etc.
我正在制作一个游戏,我计划使用所有类型的事件,具有不同的时间间隔等。
Which would be the best?
哪个最好?
采纳答案by David Andres
This article offers a fairly comprehensive explanation:
这篇文章提供了一个相当全面的解释:
"Comparing the Timer Classes in the .NET Framework Class Library" - also available as a .chm file
“比较 .NET Framework 类库中的计时器类” - 也可作为 .chm 文件使用
The specific difference appears to be that System.Timers.Timeris geared towards multithreaded applications and is therefore thread-safe via its SynchronizationObjectproperty, whereas System.Threading.Timeris ironically not thread-safe out-of-the-box.
具体的区别似乎System.Timers.Timer是面向多线程应用程序,因此通过其SynchronizationObject属性是线程安全的,而System.Threading.Timer具有讽刺意味的是,开箱即用不是线程安全的。
I don't believe that there is a difference between the two as it pertains to how small your intervals can be.
我不相信两者之间存在差异,因为它与您的间隔可以有多小有关。
回答by Tim Lovell-Smith
System.Threading.Timeris a plain timer. It calls you back on a thread pool thread (from the worker pool).
System.Threading.Timer是一个普通的计时器。它在线程池线程(从工作池中)回调您。
System.Timers.Timeris a System.ComponentModel.Componentthat wraps a System.Threading.Timer, and provides some additional features used for dispatching on a particular thread.
System.Timers.Timer是System.ComponentModel.Component包装 a 的 a System.Threading.Timer,并提供一些用于在特定线程上调度的附加功能。
System.Windows.Forms.Timerinstead wraps a native message-only-HWNDand uses Window Timersto raise events in that HWNDs message loop.
System.Windows.Forms.Timer而是包装本机仅消息 HWND并使用窗口计时器在该 HWND 消息循环中引发事件。
If your app has no UI, and you want the most light-weight and general-purpose .Net timer possible, (because you are happy figuring out your own threading/dispatching) then System.Threading.Timeris as good as it gets in the framework.
如果您的应用程序没有 UI,并且您想要尽可能轻量级和通用的 .Net 计时器(因为您很高兴找到自己的线程/调度),那么System.Threading.Timer它与框架中的一样好。
I'm not fully clear what the supposed 'not thread safe' issues with System.Threading.Timerare. Perhaps it is just same as asked in this question: Thread-safety of System.Timers.Timer vs System.Threading.Timer, or perhaps everyone just means that:
我不完全清楚所谓的“非线程安全”问题System.Threading.Timer是什么。也许它与这个问题中的问题相同:System.Timers.Timer 的线程安全 vs System.Threading.Timer,或者每个人都只是意味着:
it's easy to write race conditions when you're using timers. E.g. see this question: Timer (System.Threading) thread safety
re-entrancy of timer notifications, where your timer event can trigger and call you back a secondtime before you finish processing the firstevent. E.g. see this question: Thread-safe execution using System.Threading.Timer and Monitor
当您使用计时器时,很容易编写竞争条件。例如看这个问题: Timer (System.Threading) thread safety
计时器通知,你的计时器事件可以触发并打电话给你回的重入第二处理完之前的时间第一个事件。例如看这个问题:Thread-safe execution using System.Threading.Timer and Monitor
回答by Hero
In his book "CLR Via C#", Jeff Ritcherdiscourages using System.Timers.Timer, this timer is derived from System.ComponentModel.Component, allowing it to be used in design surface of Visual Studio. So that it would be only useful if you want a timer on a design surface.
在他的《CLR Via C#》一书中,Jeff Ritcher不鼓励使用System.Timers.Timer,这个计时器派生自System.ComponentModel.Component,允许它在 Visual Studio 的设计界面中使用。因此,只有当您想要在设计表面上使用计时器时,它才会有用。
He prefers to use System.Threading.Timerfor background tasks on a thread pool thread.
他更喜欢System.Threading.Timer在线程池线程上用于后台任务。
回答by ice1e0
Information from Microsoft about this (see Remarks on MSDN):
Microsoft 提供的有关此信息(请参阅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,它触发一个事件并定期在一个或多个事件接收器中执行代码。该类旨在用作多线程环境中的基于服务器或服务的组件;它没有用户界面,在运行时不可见。
- System.Threading.Timer,它定期在线程池线程上执行单个回调方法。回调方法是在定时器实例化时定义的,不能更改。与 System.Timers.Timer 类一样,此类旨在用作多线程环境中的基于服务器或服务组件;它没有用户界面,在运行时不可见。
- System.Windows.Forms.Timer(仅限 .NET Framework),一种 Windows 窗体组件,它定期触发事件并在一个或多个事件接收器中执行代码。该组件没有用户界面,专为在单线程环境中使用而设计;它在 UI 线程上执行。
- System.Web.UI.Timer(仅限 .NET Framework),一种 ASP.NET 组件,它定期执行异步或同步网页回发。
It is interesting to mention that System.Timers.Timerwas deprecated with .NET Core 1.0, but was implemented again in .NET Core 2.0 (/ .NET Standard 2.0).
The goal with .NET Standard 2.0 was that it should be as easy as possible to switch from the .NET Framework which is probably the reason it came back.
有趣的是,它System.Timers.Timer在 .NET Core 1.0 中已被弃用,但在 .NET Core 2.0 (/.NET Standard 2.0) 中再次实现。.NET Standard 2.0 的目标是尽可能轻松地从 .NET Framework 切换,这可能是它回归的原因。
When it was deprecated, the .NET Portability Analyzer Visual Studio Add-Inrecommended to use System.Threading.Timerinstead.
当它被弃用时,建议改用.NET Portability Analyzer Visual Studio Add-InSystem.Threading.Timer。
Looks like that Microsoft favors System.Threading.Timerbefore System.Timers.Timer.
看来System.Threading.Timer以前微软是青睐的System.Timers.Timer。
EDIT NOTE 2018-11-15:I hand to change my answer since the old information about .NET Core 1.0 was not valid anymore.
编辑说明 2018-11-15:由于有关 .NET Core 1.0 的旧信息不再有效,我决定更改我的答案。
回答by stovroz
One important difference not mentioned above which might catch you out is that System.Timers.Timersilently swallows exceptions, whereas System.Threading.Timerdoesn't.
上面没有提到的一个可能会让你注意到的重要区别是System.Timers.Timer默默地吞下异常,而System.Threading.Timer不会。
For example:
例如:
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);
回答by Default
I found a short comparison from MSDN
我从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 Framework 类库包括四个名为 Timer 的类,每个类都提供不同的功能:
System.Timers.Timer,它会触发一个事件并定期在一个或多个事件接收器中执行代码。该类旨在用作多线程环境中的基于服务器或服务的组件;它没有用户界面,在运行时不可见。
System.Threading.Timer,它定期在线程池线程上执行单个回调方法。回调方法是在定时器实例化时定义的,不能更改。与 System.Timers.Timer 类一样,此类旨在用作多线程环境中的基于服务器或服务组件;它没有用户界面,在运行时不可见。
System.Windows.Forms.Timer,一种 Windows 窗体组件,它触发事件并定期在一个或多个事件接收器中执行代码。该组件没有用户界面,专为在单线程环境中使用而设计。
System.Web.UI.Timer,一个 ASP.NET 组件,它定期执行异步或同步网页回发。
回答by Edward Brey
The two classes are functionally equivalent, except that System.Timers.Timerhas an option to invoke all its timer expiration callbacks through ISynchronizeInvokeby setting SynchronizingObject. Otherwise, both timers invoke expiration callbacks on thread pool threads.
这两个类在功能上是等效的,除了可以System.Timers.Timer选择通过设置SynchronizingObject通过ISynchronizeInvoke调用其所有计时器到期回调。否则,两个计时器都会在线程池线程上调用到期回调。
When you drag a System.Timers.Timeronto a Windows Forms design surface, Visual Studio sets SynchronizingObjectto the form object, which causes all expiration callbacks to be called on the UI thread.
将 aSystem.Timers.Timer拖到 Windows 窗体设计图面上时,Visual Studio 将SynchronizingObject设置为窗体对象,这会导致在 UI 线程上调用所有到期回调。
回答by Greg Gum
From MSDN: System.Threading.Timeris a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timeris a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
来自 MSDN:System.Threading.Timer是一个简单的轻量级计时器,它使用回调方法并由线程池线程提供服务。不建议将它用于 Windows 窗体,因为它的回调不会发生在用户界面线程上。System.Windows.Forms.Timer是与 Windows 窗体一起使用的更好选择。对于基于服务器的计时器功能,您可以考虑使用System.Timers.Timer,它会引发事件并具有其他功能。

