Share via


정기 작업 항목 만들기

중요 API

주기적으로 반복되는 작업 항목을 만드는 방법을 알아봅니다.

정기 작업 항목 만들기

CreatePeriodicTimer 메서드를 사용하여 정기적인 작업 항목을 만듭니다. 작업을 완수하는 람다를 공급하고 기간 매개 변수를 사용하여 공급 사이의 간격을 지정합니다. 기간은 TimeSpan 구조를 사용하여 지정됩니다. 작업 항목은 각 기간이 경과할 때마다 다시 제출되므로 작업이 완료될 수 있을 만큼 기간이 긴지 확인합니다.

CreateTimerThreadPoolTimer 개체를 반환합니다. 타이머를 취소해야 하는 경우 이 개체를 저장합니다.

참고 시간 간격에 0(또는 1밀리초 미만의 값)을 지정하지 마세요. 이렇게 하면 기간 타이머 대신 단일 샷 타이머처럼 작동합니다.

참고, 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 오버로드를 사용하여 정기작업 항목의 취소를 처리하는 추가 람다를 제공합니다.

다음 예제는 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();

설명

일회용 타이머에 대한 자세한 내용은 타이머를 사용하여 작업 항목 제출 을 참조하세요.