『WPF』Timer的使用

几点说明


  1. 由于正在做的一个小项目需要使用计时的功能,所以有了这个Timer使用的文章
  2. 这篇的内容主要参考自MSDN中有关Timer使用的说明
  3. 以后如果遇到更高层次的使用方法,会继续更新这篇文章

来自MSDN的说明


System.Timers.Timer

  Name Description
Method Timer
「构造方法」
Initializes a new instance of the Timer class, and sets all the properties to their initial values.
初始化一个Timer类的新实例,以及设置初始值,精确度为
Method Timer(Double)
「构造方法」
Initializes a new instance of the Timer class, and sets the Interval property to the specified number of milliseconds.
初始化一个Timer类的新实例,以及设置初始值,精确度为毫秒
Property AutoReset
「自动重置」
Gets or sets a value indicating whether the Timer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses.
获取或设置一个值指示 Timer 是「每次都调用Elapsed 事件」还是「只有第一次调用」。
Property SynchronizingObject
「同步对象」
Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed.
获取或设置用于整理超过设置的时间间隔的事件调用。
Method Start
「Public」
Starts raising the Elapsed event by setting Enabled to true.
通过设置Enalbed为true,马上开始调用Elapsed
Method Stop
「Public」
Stops raising the Elapsed event by setting Enabled to false.
通过设置Enalbed为false,马上停止调用Elapsed
Event Elapsed
「流逝」
Occurs when the interval elapses.
当时间间隔「流逝」时触发。

The Timer component is a server-based timer, which allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing.

Timer组件是一个基于服务器的计时器,它允许你指定一个有间隔的「调用」Elapsed事件的循环。你可以捕获这个事件来进行你的处理。

Note: When AutoReset is set to false, the Timer raises the Elapsed event only once, after the first Interval has elapsed. To keep raising the Elapsed event on the Interval, set AutoReset to true.

AutoReset属性设置为false时,Timer在第一次时间间隔到了之后,只调用一次Elapsed事件。为了保证每次时间间隔都调用Elapsed事件,要设置AutoReset属性为true

If the SynchronizingObject property is Nothing, the Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In this situation, the event handler should be reentrant.

如果SynchronizingObject 属性没有设置, Elapsed事件被一个线程池线程调用。如果Elapsed事件持续的时间比一次间隔要长,这个事件可能会在另一个线程池的线程被调用。在这种情况下,事件处理应该是可重复的。

Note: The event-handling method might run on one thread at the same time that another thread calls the Stop method or sets the Enabled property to false. This might result in the Elapsed event being raised after the timer is stopped.

当一个线程调用Stop方法或设置Enabled属性为false时,相应的事件处理方法可能会在不同的线程中继续执行。这可能导致Elapsed事件在计时器已经被停止的时候继续执行。

Even if SynchronizingObject is not Nothing, Elapsed events can occur after the Dispose or Stop method has been called or after the Enabled property has been set to false, because the signal to raise the Elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the Elapsed event to ignore subsequent events.

即使SynchronizingObject 已经设置了,Elapsed事件还是会发生在Dispose或Stop方法被调用之后或在Enabled属性被设置成false后,因为调用Elapsed事件的「符号」总是在一个线程池中列队执行。一个解决这个竞争条件的方法是设置一个「标识」告诉Elapsed事件处理程序忽略掉随后的事件。

TimerWindow

image

TimerWindow的XAML内容
TimerWindow的后台代码

说明:

  1. 注意之前说过的Timer Elapsed事件的线程有关的问题
  2. 注意,你可以在任何你想开始计时的地方调用Timer的Start()方法开始计时,调用Stop()方法停止计时。


运行效果


image

原文地址:https://www.cnblogs.com/sitemanager/p/2356594.html