次の方法で共有


How to create a periodic work item (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Learn how to create a work item that repeats periodically.

Technologies

Instructions

Step 1: Create the periodic work item

Use the CreatePeriodicTimer method to create a periodic work item. Supply a lambda that accomplishes the work, and use the period parameter to specify the interval between submissions. The period is specified using a TimeSpan structure. The work item will be resubmitted every time the period elapses, so make sure the period is long enough for work to complete.

CreateTimer returns a ThreadPoolTimer object. Store this object in case the timer needs to be canceled.

Note  Avoid specifying a value of zero (or any value less than one millisecond) for the interval. This causes the periodic timer to behave as a single-shot timer instead.

 

Note  You can use CoreDispatcher.RunAsync to access the UI and show progress from the work item.

 

The following example creates a work item that runs once every 60 seconds:

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);

Step 2: Handle cancellation of the periodic work item (optional)

If needed, you can handle cancellation of the periodic timer with a TimerDestroyedHandler. Use the CreatePeriodicTimer overload to supply an additional lambda that handles cancellation of the periodic work item.

The following example creates a periodic work item that repeats every 60 seconds and it also supplies a cancellation handler:

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.

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

                }));
        }));

Step 3: Cancel the timer

When necessary, call the Cancel method to stop the periodic work item from repeating. If the work item is running when the periodic timer is cancelled it is allowed to complete. The TimerDestroyedHandler (if provided) is called when all instances of the periodic work item have completed.

PeriodicTimer.Cancel();
PeriodicTimer->Cancel();

Remarks

For information about single-use timers, see How to submit a work item using a timer.

See the thread pool sample for a complete code sample that demonstrates work items, timer work items, and periodic work items.

Quickstart: Submitting a work item to the thread pool

How to submit a work item using a timer

How to create and use pre-allocated work items

How to respond to named events and semaphores

How to use functions as work item delegates

Best practices for using the thread pool