DispatcherTimer クラス

定義

Dispatcher キューに統合されるタイマー。このタイマーは、指定した間隔で、指定した優先度で処理されます。

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

次の例では、DispatcherTimerの内容を更新し、LabelInvalidateRequerySuggested メソッドを呼び出すCommandManagerを作成します。

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を更新し、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) スレッド上のオブジェクトにアクセスするには、DispatcherまたはInvokeを使用して、ユーザー インターフェイス (UI) スレッドのBeginInvokeに操作をポストする必要があります。 DispatcherTimerではなくSystem.Timers.Timerを使用する理由は、DispatcherTimerDispatcherと同じスレッドで実行され、DispatcherPriorityDispatcherTimerを設定できることです。

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

タイマー間隔が経過したときに発生します。

適用対象