DispatcherTimer 類別

定義

整合至 Dispatcher 佇列中的計時器,會在指定的時間間隔以指定的優先權處理這個佇列。

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

範例

下列範例會建立 , DispatcherTimer 以更新 的內容 Label ,並在 上 CommandManager 呼叫 InvalidateRequerySuggested 方法。

建立 DispatcherTimer 名為 dispatcherTimer 的物件。 事件處理常式 dispatcherTimer_Tick 會新增至 TickdispatcherTimer 事件。 會 Interval 使用 TimeSpan 物件設定為 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 顯示目前秒的 ,並在 上 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 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當作業執行取決於佇列中的其他作業及其優先順序時。

System.Timers.Timer如果在 WPF 應用程式中使用 ,值得注意的是,在 System.Timers.Timer 與使用者介面 (UI) 執行緒不同的執行緒上執行 。 若要存取使用者介面 (UI) 執行緒上的物件,您必須使用 InvokeBeginInvoke 將作業張貼至 Dispatcher 使用者介面的 (UI) 執行緒。 使用 DispatcherTimer 與 相反 System.Timers.Timer 的原因是,在 DispatcherTimer 與 相同的執行緒 Dispatcher 上執行 ,而且 DispatcherPriority 可以在 上 DispatcherTimer 設定 。

每當 DispatcherTimer 物件的方法系結至計時器時,就會讓物件保持運作。

建構函式

DispatcherTimer()

初始化 DispatcherTimer 類別的新執行個體。

DispatcherTimer(DispatcherPriority)

初始化 DispatcherTimer 類別的新執行個體,這個類別會以指定的優先權處理計時器事件。

DispatcherTimer(DispatcherPriority, Dispatcher)

初始化 DispatcherTimer 類別的新執行個體,這個類別會以指定的優先權執行於指定之 Dispatcher 上。

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

發生於計時器間隔已耗用時。

適用於