创建定期工作项

重要的 API

了解如何创建定期重复的工作项。

创建定期工作项

使用 CreatePeriodicTimer 方法创建定期工作项。 提供一个执行该工作的 lambda 表达式,并使用 period 参数指定两次提交之间的间隔。 使用 TimeSpan 结构指定该时间段。 每当该时间间隔结束时,系统都会重新提交该工作项,因此请确保该时间间隔足够长,以使工作项能够完成。

CreatePeriodicTimer 返回 ThreadPoolTimer 对象。 如果计时器需要取消,请存储此对象。

注释

避免为间隔指定零值(或小于 1 毫秒的任何值)。 这将导致定期计时器像单次计时器一样操作。

注释

在 WinUI 3 应用中,使用 DispatcherQueue.TryEnqueueMicrosoft.UI.Dispatching.DispatcherQueue 中访问 UI 线程并显示工作项的进度。 有关详细信息,请参阅 DispatcherQueue 概述。

以下示例创建每 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();

注解

有关单用计时器的信息,请参阅 使用计时器提交工作项