DispatcherTimer 类

定义

集成到队列中的 Dispatcher 计时器,该队列按指定的时间间隔和指定优先级进行处理。

public ref class DispatcherTimer
public class DispatcherTimer
type DispatcherTimer = class
Public Class DispatcherTimer
继承
DispatcherTimer

示例

以下示例创建一个更新方法的内容并在 <a0/> 上调用方法的 a

将创建一个名为DispatcherTimerdispatcherTimer的对象。 dispatcherTimer_Tick事件处理程序将添加到Tick事件dispatcherTimer中。 使用IntervalTimeSpan对象将设置为 1 秒,并启动计时器。

//  DispatcherTimer setup
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Start();
'  DispatcherTimer setup
dispatcherTimer = New Threading.DispatcherTimer()
AddHandler dispatcherTimer.Tick, AddressOf dispatcherTimer_Tick
dispatcherTimer.Interval = New TimeSpan(0,0,1)
dispatcherTimer.Start()

Tick事件处理程序更新显示当前秒的一个Label,并调用InvalidateRequerySuggestedCommandManager

//  System.Windows.Threading.DispatcherTimer.Tick handler
//
//  Updates the current seconds display and calls
//  InvalidateRequerySuggested on the CommandManager to force 
//  the Command to raise the CanExecuteChanged event.
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // Updating the Label which displays the current second
    lblSeconds.Content = DateTime.Now.Second;

    // Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested();
}
'  System.Windows.Threading.DispatcherTimer.Tick handler
'
'  Updates the current seconds display and calls
'  InvalidateRequerySuggested on the CommandManager to force 
'  the Command to raise the CanExecuteChanged event.
Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    ' Updating the Label which displays the current second
    lblSeconds.Content = Date.Now.Second

    ' Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested()
End Sub

注解

在每个 DispatcherTimer 循环的 Dispatcher 顶部重新计算该函数。

不保证计时器在发生时间间隔时完全执行,但保证在时间间隔发生之前不执行计时器。 这是因为 DispatcherTimer 操作与其他操作一样放置在队列上 DispatcherDispatcherTimer执行操作时依赖于队列中的其他作业及其优先级。

如果在WPF应用程序中使用了 System.Timers.Timer,则值得注意的是,System.Timers.Timer在用户界面 (UI) 线程以外的线程上运行。 若要访问用户界面 (UI) 线程上的对象,必须使用或将Dispatcher操作Invoke发布到用户界面 (UI) 线程BeginInvoke上。 使用与 < a0/> 相同的线程上运行的原因与在 上可以设置一个

每当对象的方法绑定到计时器时,A DispatcherTimer 都会使对象保持活动状态。

构造函数

名称 说明
DispatcherTimer()

初始化 DispatcherTimer 类的新实例。

DispatcherTimer(DispatcherPriority, Dispatcher)

初始化在指定优先级处运行的DispatcherTimer类的新实例Dispatcher

DispatcherTimer(DispatcherPriority)

初始化类的新实例,该实例 DispatcherTimer 处理指定优先级处的计时器事件。

DispatcherTimer(TimeSpan, DispatcherPriority, EventHandler, Dispatcher)

初始化类的新实例,该实例 DispatcherTimer 使用指定的时间间隔、优先级、事件处理程序和 Dispatcher

属性

名称 说明
Dispatcher

获取与此Dispatcher关联的值DispatcherTimer

Interval

获取或设置计时器时钟周期之间的时间段。

IsEnabled

获取或设置一个值,该值指示计时器是否正在运行。

Tag

获取或设置用户定义的数据对象。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Start()

启动 。DispatcherTimer

Stop()

停止 .DispatcherTimer

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

活动

名称 说明
Tick

在计时器间隔已过时发生。

适用于