DispatcherTimer 類別
本文內容
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
整合至 Dispatcher 佇列中的計時器,會在指定的時間間隔以指定的優先權處理這個佇列。
public ref class DispatcherTimer
public class DispatcherTimer
type DispatcherTimer = class
Public Class DispatcherTimer
- 繼承
-
DispatcherTimer
下列範例會建立 , DispatcherTimer 以更新 的內容 Label ,並在 上 CommandManager 呼叫 InvalidateRequerySuggested 方法。
建立 DispatcherTimer 名為 dispatcherTimer
的物件。 事件處理常式 dispatcherTimer_Tick
會新增至 Tick 的 dispatcherTimer
事件。 會 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 作業會像其他作業一樣放在佇列上 Dispatcher 。 DispatcherTimer當作業執行取決於佇列中的其他作業及其優先順序時。
System.Timers.Timer如果在 WPF 應用程式中使用 ,值得注意的是,在 System.Timers.Timer 與使用者介面 (UI) 執行緒不同的執行緒上執行 。 若要存取使用者介面 (UI) 執行緒上的物件,您必須使用 Invoke 或 BeginInvoke 將作業張貼至 Dispatcher 使用者介面的 (UI) 執行緒。 使用 DispatcherTimer 與 相反 System.Timers.Timer 的原因是,在 DispatcherTimer 與 相同的執行緒 Dispatcher 上執行 ,而且 DispatcherPriority 可以在 上 DispatcherTimer 設定 。
每當 DispatcherTimer 物件的方法系結至計時器時,就會讓物件保持運作。
Dispatcher |
初始化 DispatcherTimer 類別的新執行個體。 |
Dispatcher |
初始化 DispatcherTimer 類別的新執行個體,這個類別會以指定的優先權處理計時器事件。 |
Dispatcher |
初始化 DispatcherTimer 類別的新執行個體,這個類別會以指定的優先權執行於指定之 Dispatcher 上。 |
Dispatcher |
初始化這個 DispatcherTimer 類別的新執行個體,這個類別會使用指定的時間間隔、優先權、事件處理常式和 Dispatcher。 |
Dispatcher |
取得與這個 Dispatcher 關聯的 DispatcherTimer。 |
Interval |
取得或設定計時器刻度之間的時間間隔。 |
Is |
取得或設定值,這個值表示計時器是否正在執行。 |
Tag |
取得或設定使用者定義的資料物件。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Start() |
啟動 DispatcherTimer。 |
Stop() |
停止 DispatcherTimer。 |
To |
傳回代表目前物件的字串。 (繼承來源 Object) |
Tick |
發生於計時器間隔已耗用時。 |
產品 | 版本 |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |