建立定期工作項目

重要 API

了解如何建立定期重複的工作項目。

建立定期工作項目

使用 CreatePeriodicTimer 方法建立定期工作項目。 提供完成工作的 lambda,並使用 period 參數指定提交之間的間隔。 此週期是使用 TimeSpan 結構指定的。 每次該時段過去後,工作項目都會重新提交,因此請確保該時間段足夠長以便工作能夠完成。

CreateTimer 會傳回 ThreadPoolTimer 物件。 儲存此物件,以防需要取消計時器。

注意 避免將間隔的值設為零 (或任何小於一毫秒的值)。 這會導致定期計時器改為以單次計時器的形式運作。

注意 您可以使用 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 重載提供額外的 lambda 來處理取消定期工作項目。

以下範例會建立一個每 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();

備註

有關一次性計時器的資訊,請參閱使用計時器提交工作項目