DispatcherTimer 類別

定義

提供整合至 發送器 佇列的計時器,此佇列會以指定的時間間隔和指定的優先順序進行處理。

/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class DispatcherTimer
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class DispatcherTimer
Public Class DispatcherTimer
繼承
Object IInspectable DispatcherTimer
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

此範例程式碼會實作簡單的主控台樣式計時器,將資料寫入名為 (XAML 的 TimerLogTextBlock,該 XAML TimerLog 不會顯示) 。 Interval值會設定為 1,而記錄會顯示每個Tick的實際經過時間。

DispatcherTimer dispatcherTimer;
DateTimeOffset startTime;
DateTimeOffset lastTime;
DateTimeOffset stopTime;
int timesTicked = 1;
int timesToTick = 10;

public void DispatcherTimerSetup()
{
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    //IsEnabled defaults to false
    TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
    startTime = DateTimeOffset.Now;
    lastTime = startTime;
    TimerLog.Text += "Calling dispatcherTimer.Start()\n";
    dispatcherTimer.Start();
    //IsEnabled should now be true after calling start
    TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
}

void dispatcherTimer_Tick(object sender, object e)
{
    DateTimeOffset time = DateTimeOffset.Now;
    TimeSpan span = time - lastTime;
    lastTime = time;
    //Time since last tick should be very very close to Interval
    TimerLog.Text += timesTicked + "\t time since last tick: " + span.ToString() + "\n";
    timesTicked++;
    if (timesTicked > timesToTick)
    {
        stopTime = time;
        TimerLog.Text += "Calling dispatcherTimer.Stop()\n";
        dispatcherTimer.Stop();
        //IsEnabled should now be false after calling stop
        TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
        span = stopTime - startTime;
        TimerLog.Text += "Total Time Start-Stop: " + span.ToString() + "\n";
    }
}
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
    DispatcherTimerSetup();
}
// MainPage.cpp
...
#include <chrono>
...
void MainPage::StartTimerAndRegisterHandler()
{
    Windows::UI::Xaml::DispatcherTimer timer;
    timer.Interval(std::chrono::milliseconds{ 500 });
    auto registrationtoken = timer.Tick({this, &MainPage::OnTick });
    timer.Start();
}

void MainPage::OnTick(Windows::Foundation::IInspectable const& /* sender */,
    Windows::Foundation::IInspectable const& /* e */)
{
    // do something on each tick here ...
}
// .cpp definition, .h not shown
void MainPage::StartTimerAndRegisterHandler() {
    auto timer = ref new Windows::UI::Xaml::DispatcherTimer();
    TimeSpan ts;
    ts.Duration = 500;
    timer->Interval = ts;
    timer->Start();
    auto registrationtoken = timer->Tick += ref new EventHandler<Object^>(this,&MainPage::OnTick);
}
void MainPage::OnTick(Object^ sender, Object^ e) {
    // do something on each tick here ...
}

備註

DispatcherTimer 可用來在產生 UI 執行緒的相同執行緒上執行程式碼。 在此執行緒上執行的程式碼有權建立和修改只能在 UI 執行緒上建立和修改的物件。 若要指定程式碼應該在 UI 執行緒上執行,請設定 Interval 屬性,然後呼叫 Start 方法。 Tick事件會在Interval中指定的時間經過之後引發。 刻度 會繼續以相同的 間隔 引發,直到呼叫 Stop 方法、應用程式終止或應用程式暫停, (引發 Suspending) 。

DispatcherTimer 的其中一個案例是檢查感應器上的屬性,其中感應器值的變更並非純事件驅動,或事件不會提供您想要的資料細微性。 您可以在 加速計範例中看到此專案。

DispatcherTimer 的其他案例包括檢查沒有相關事件的狀態變更,或針對無法使用分鏡腳本動畫或雙向系結的定期 UI 更新。

提示

如果您要移轉 Microsoft Silverlight 或Windows Presentation Foundation (WPF) 程式碼,DispatcherTimer 和相關發送器位於個別的 System.Windows.Threading命名空間中。 Windows 執行階段中沒有Windows.UI.Xaml.Threading命名空間,因此這個類別位於Windows.UI.Xaml中。

如果您未在 Tick 處理常式中使用 UI 執行緒執行任何動作,而且只需要計時器,您也可以改用 ThreadPoolTimer 。 此外,對於 ThreadPoolTimer 或 .NET 工作等技術,您不會與 UI 執行緒完全隔離。 您仍然可以使用 CoreDispatcher.RunAsync 以非同步方式指派給 UI 執行緒。

建構函式

DispatcherTimer()

初始化 DispatcherTimer 類別的新實例。

屬性

Interval

取得或設定計時器刻度之間的時間量。

IsEnabled

取得值,這個值表示計時器是否正在執行。

方法

Start()

啟動 DispatcherTimer

Stop()

停止 DispatcherTimer

事件

Tick

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

適用於

另請參閱