次の方法で共有


定期的な作業項目の作成

重要な API

定期的に実行される作業項目の作成方法を説明します。

定期的な作業項目の作成

定期的な作業項目を作成するには、CreatePeriodicTimer メソッドを使います。 作業を実行するラムダを指定し、period パラメーターを使って送信の間隔を指定します。 period パラメーターは TimeSpan 構造体を使って指定します。 この期間が経過するたびに作業項目が再送信されるため、作業を完了できる十分な長さを確保してください。

CreateTimerThreadPoolTimer オブジェクトを返します。 タイマーを取り消す必要が生じた場合は、このオブジェクトを格納します。

間隔の値を 0 にする (または 1 ミリ秒未満の値にする) ことは避けてください。 この場合、定期タイマーは 1 回限りのタイマーとして動作します。

CoreDispatcher.RunAsync を使って UI にアクセスしたり、作業項目の進捗状況を表示したりすることができます。

次の例では、60 秒ごとに 1 回実行される作業項目を作成します。

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();

注釈

1 回限りのタイマーについて詳しくは、「タイマーを使った作業項目の送信」をご覧ください。