DispatcherTimer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
一個整合於 Dispatcher 佇列中的計時器,會在指定時間間隔和優先順序下處理。
public ref class DispatcherTimer
public class DispatcherTimer
type DispatcherTimer = class
Public Class DispatcherTimer
- 繼承
-
DispatcherTimer
範例
以下範例建立 aDispatcherTimer,更新 a Label 的內容,並呼叫 InvalidateRequerySuggested 該方法。CommandManager
會建立一個 DispatcherTimer 被命名的 dispatcherTimer 物件。 事件處理 dispatcherTimer_Tick 程序會被 Tick 加入事件 dispatcherTimer中。
Interval將 設定為 1 秒,使用TimeSpan物件,計時器啟動。
// 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事件處理器會更新顯示當前秒數的 aLabel,並呼叫 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 操作會像其他操作一樣被放在隊列中 Dispatcher 。 操作執行時間 DispatcherTimer 取決於隊列中其他工作及其優先順序。
若WPF應用程式中使用 System.Timers.Timer,值得注意的是 System.Timers.Timer 執行於與使用者介面(UI)執行緒不同的執行緒。 為了存取使用者介面(UI)執行緒上的物件,必須將操作貼入使用者介面(UI)執行緒的 , Dispatcher 使用 Invoke 或 BeginInvoke。 使用 a DispatcherTimer 而非 a System.Timers.Timer 的原因是,DispatcherTimer與 和 a 在同一執行緒DispatcherDispatcherPriority上的執行可以設定在 DispatcherTimer。
只要物件的方法綁定到計時器,A DispatcherTimer 就會讓該物件保持存活。
建構函式
| 名稱 | Description |
|---|---|
| DispatcherTimer() |
初始化 DispatcherTimer 類別的新執行個體。 |
| DispatcherTimer(DispatcherPriority, Dispatcher) |
初始化一個新的類別實例,該實例 DispatcherTimer 在指定 Dispatcher 優先順序上執行。 |
| DispatcherTimer(DispatcherPriority) |
初始化一個新的類別實例 DispatcherTimer ,處理指定優先順序的計時器事件。 |
| DispatcherTimer(TimeSpan, DispatcherPriority, EventHandler, Dispatcher) |
初始化一個新的類別實例 DispatcherTimer ,使用指定的時間區間、優先權、事件處理程序和 Dispatcher。 |
屬性
| 名稱 | Description |
|---|---|
| Dispatcher |
這與 Dispatcher 此 DispatcherTimer有關。 |
| Interval |
讀取或設定計時器滴答之間的時間間隔。 |
| IsEnabled |
取得或設定一個值,指示計時器是否正在運行。 |
| Tag |
取得或設定使用者定義的資料物件。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Start() |
開始 DispatcherTimer。 |
| Stop() |
停止 DispatcherTimer. |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
事件
| 名稱 | Description |
|---|---|
| Tick |
當計時器間隔結束時發生。 |