使用計時器提交工作項目

重要 API

了解如何建立在計時器經過之後執行的工作項目。

建立單次計時器

使用 CreateTimer 方法來未工作項目建立計時器。 提供完成工作的 lambda,並使用 delay 參數指定執行緒集區在將工作項目指派給可用執行緒之前的等待時間。 此延遲是使用 TimeSpan 結構指定的。

注意 您可以使用 CoreDispatcher.RunAsync 來存取 UI 並顯示工作項目的進度。

以下範例會建立一個在三分鐘內執行的工作項目:

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 多載來提供額外的 Lambda。 這會在計時器取消或工作項目完成時執行。

以下範例會建立提交工作項目的計時器,並在工作項目完成或取消計時器時呼叫一個方法:

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.
                    }

                }));
        }));

取消計時器

如果計時器仍在倒數計時,但不再需要執行工作項目,請呼叫取消。 計時器即會取消,且工作項目不會提交到執行緒集區。

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

備註

通用 Windows 平台 (UWP) 應用程式無法使用 Thread.Sleep,因為它可能會阻塞 UI 執行緒。 您可以改用 ThreadPoolTimer 來建立工作項目,這將會延遲工作項目完成的工作,而不會阻塞 UI 執行緒。

有關示範工作項目、計時器工作項目和定期工作項目的完整程式碼範例,請參閱執行緒集區範例。 這個程式碼範例最初是為 Windows 8.1 撰寫的,但該程式碼可以在 Windows 10 中重複使用。

有關重複計時器的資訊,請參閱建立定期工作項目