共用方式為


使用計時器提交工作項目

重要的應用程式介面

瞭解如何建立在計時器過期後執行的工作項目。

建立單次定時器

使用 CreateTimer 方法來建立工作專案的定時器。 提供可完成工作的 Lambda,並使用 延遲 參數來指定線程集區等候的時間長度,才能將工作專案指派給可用的線程。 延遲是使用 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.
                    }

                }));
        }));

取消定時器

如果定時器仍在倒數,但不再需要工作專案,請呼叫 Cancel。 定時器已取消,且工作專案不會提交至線程集區。

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

備註

通用 Windows 平臺 (UWP) app 無法使用 Thread.Sleep,因為它可以封鎖 UI 線程。 您可以使用 ThreadPoolTimer 來建立工作項目,這將延遲工作項目的執行而不會封鎖 UI 執行緒。

如需示範工作專案、定時器工作專案和定期工作專案的完整程式碼範例,請參閱 線程集區範例。 程序代碼範例原本是針對 Windows 8.1 撰寫,但程式代碼可以在 Windows 10 中重複使用。

如需有關重複計時器的相關資訊,請參閱建立定期工作專案