重要な API
定期的に繰り返される作業項目を作成する方法について説明します。
定期的な作業項目を作成する
定期的な作業項目を作成するには、 CreatePeriodicTimer メソッドを使用します。 作業を実行するラムダを指定し、 period パラメーターを使用して送信間の間隔を指定します。 期間は 、TimeSpan 構造体を使用して指定します。 作業項目は、期間が経過するたびに再送信されるため、作業が完了するのに十分な期間であることを確認してください。
CreateTimer はThreadPoolTimer オブジェクトを 返します。 タイマーを取り消す必要がある場合は、このオブジェクトを格納します。
手記 間隔に 0 (または 1 ミリ秒未満の値) を指定しないでください。 これにより、定期的なタイマーが代わりにシングルショット タイマーとして動作します。
手記CoreDispatcher.RunAsync を使用して UI にアクセスし、作業項目の進行状況を表示できます。
次の例では、60 秒ごとに実行される作業項目を作成します。
TimeSpan period = TimeSpan.FromSeconds(60);
ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher.RunAsync(CoreDispatcherPriority.High,
() =>
{
//
// UI components can be accessed within this scope.
//
});
}, period);
TimeSpan period;
period.Duration = 60 * 10000000; // 10,000,000 ticks per second
ThreadPoolTimer ^ PeriodicTimer = ThreadPoolTimer::CreatePeriodicTimer(
ref new TimerElapsedHandler([this](ThreadPoolTimer^ source)
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([this]()
{
//
// UI components can be accessed within this scope.
//
}));
}), period);
定期的な作業項目の取り消しを処理する (省略可能)
必要に応じて、 TimerDestroyedHandler を使用して定期的なタイマーのキャンセルを処理できます。 定期的な作業項目の取り消しを処理する追加のラムダを指定するには 、CreatePeriodicTimer オーバーロードを使用します。
次の例では、60 秒ごとに繰り返される定期的な作業項目を作成し、キャンセル ハンドラーも提供します。
using Windows.System.Threading;
TimeSpan period = TimeSpan.FromSeconds(60);
ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher.RunAsync(CoreDispatcherPriority.High,
() =>
{
//
// UI components can be accessed within this scope.
//
});
},
period,
(source) =>
{
//
// TODO: Handle periodic timer cancellation.
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher->RunAsync(CoreDispatcherPriority.High,
()=>
{
//
// UI components can be accessed within this scope.
//
// Periodic timer cancelled.
}));
});
using namespace Windows::System::Threading;
using namespace Windows::UI::Core;
TimeSpan period;
period.Duration = 60 * 10000000; // 10,000,000 ticks per second
ThreadPoolTimer ^ PeriodicTimer = ThreadPoolTimer::CreatePeriodicTimer(
ref new TimerElapsedHandler([this](ThreadPoolTimer^ source)
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([this]()
{
//
// UI components can be accessed within this scope.
//
}));
}),
period,
ref new TimerDestroyedHandler([&](ThreadPoolTimer ^ source)
{
//
// TODO: Handle periodic timer cancellation.
//
Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([&]()
{
//
// UI components can be accessed within this scope.
//
// Periodic timer cancelled.
}));
}));
タイマーを取り消す
必要に応じて、 Cancel メソッドを呼び出して、定期的な作業項目の繰り返しを停止します。 定期的なタイマーが取り消されたときに作業項目が実行されている場合は、完了が許可されます。 TimerDestroyedHandler (指定されている場合) は、定期的な作業項目のすべてのインスタンスが完了したときに呼び出されます。
PeriodicTimer.Cancel();
PeriodicTimer->Cancel();
注釈
単一使用タイマーの詳細については、「 タイマーを使用して作業項目を送信する」を参照してください。
関連トピック
- スレッド プールに作業項目を送信
- スレッド プール を使用するためのベスト プラクティス
- タイマーを使用して作業項目を送信