次の方法で共有


タイマーを使用して作業項目を送信する

重要な API

タイマーが経過した後に実行される作業項目を作成する方法について説明します。

シングルショット タイマーを作成する

CreateTimer メソッドを使用して、作業項目のタイマーを作成します。 作業を実行するラムダを指定し、 delay パラメーターを使用して、使用可能なスレッドに作業項目を割り当てる前にスレッド プールが待機する時間を指定します。 遅延は TimeSpan 構造体を使用して指定されます。

手記CoreDispatcher.RunAsync を使用して UI にアクセスし、作業項目の進行状況を表示できます。

次の例では、3 分で実行される作業項目を作成します。

TimeSpan delay = TimeSpan.FromMinutes(3);
            
ThreadPoolTimer DelayTimer = ThreadPoolTimer.CreateTimer(
    (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.
                //

            });

    }, delay);
TimeSpan delay;
delay.Duration = 3 * 60 * 10000000; // 10,000,000 ticks per second

ThreadPoolTimer ^ DelayTimer = ThreadPoolTimer::CreateTimer(
        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.
                    //

                    ExampleUIUpdateMethod("Timer completed.");

                }));

        }), delay);

完了ハンドラーを指定する

必要に応じて、 TimerDestroyedHandler を使用して作業項目の取り消しと完了を処理します。 CreateTimer オーバーロードを使用して、追加のラムダを指定します。 これは、タイマーが取り消されたとき、または作業項目が完了したときに実行されます。

次の例では、作業項目を送信するタイマーを作成し、作業項目が終了するかタイマーが取り消されたときにメソッドを呼び出します。

TimeSpan delay = TimeSpan.FromMinutes(3);
            
bool completed = false;

ThreadPoolTimer DelayTimer = ThreadPoolTimer.CreateTimer(
    (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.
                    //

                });

        completed = true;
    },
    delay,
    (source) =>
    {
        //
        // TODO: Handle work cancellation/completion.
        //


        //
        // Update the UI thread by using the UI core dispatcher.
        //
        Dispatcher.RunAsync(
            CoreDispatcherPriority.High,
            () =>
            {
                //
                // UI components can be accessed within this scope.
                //

                if (completed)
                {
                    // Timer completed.
                }
                else
                {
                    // Timer cancelled.
                }

            });
    });
TimeSpan delay;
delay.Duration = 3 * 60 * 10000000; // 10,000,000 ticks per second

completed = false;

ThreadPoolTimer ^ DelayTimer = ThreadPoolTimer::CreateTimer(
        ref new TimerElapsedHandler([&](ThreadPoolTimer ^ source)
        {
            //
            // TODO: Work
            //

            //
            // Update the UI thread by using the UI core dispatcher.
            //
            Dispatcher->RunAsync(CoreDispatcherPriority::High,
                ref new DispatchedHandler([&]()
                {
                    //
                    // UI components can be accessed within this scope.
                    //

                }));

            completed = true;

        }),
        delay,
        ref new TimerDestroyedHandler([&](ThreadPoolTimer ^ source)
        {
            //
            // TODO: Handle work cancellation/completion.
            //

            Dispatcher->RunAsync(CoreDispatcherPriority::High,
                ref new DispatchedHandler([&]()
                {
                    //
                    // Update the UI thread by using the UI core dispatcher.
                    //

                    if (completed)
                    {
                        // Timer completed.
                    }
                    else
                    {
                        // Timer cancelled.
                    }

                }));
        }));

タイマーを取り消す

タイマーがまだカウントダウンしているが、作業項目が不要になった場合は、Cancel を呼び出 します。 タイマーは取り消され、作業項目はスレッド プールに送信されません。

DelayTimer.Cancel();
DelayTimer->Cancel();

注釈

ユニバーサル Windows プラットフォーム (UWP) アプリでは、UI スレッドをブロックできるため、 Thread.Sleep を使用できません。 代わりに ThreadPoolTimer を使用して作業項目を作成できます。これにより、UI スレッドをブロックせずに作業項目によって実行されるタスクが遅延します。

作業項目、タイマー作業項目、および定期的な作業項目を示す完全なコード サンプルについては、 スレッド プールのサンプル を参照してください。 このコード サンプルはもともと Windows 8.1 用に作成されましたが、Windows 10 でコードを再利用できます。

繰り返しタイマーの詳細については、「 定期的な作業項目を作成する」を参照してください。