주요 API들
타이머가 경과한 후 실행되는 작업 항목을 만드는 방법을 알아봅니다.
단일 샷 타이머 만들기
CreateTimer 메서드를 사용하여 작업 항목에 대한 타이머를 만듭니다. 작업을 수행하는 람다를 제공하고 , 지연 매개 변수를 사용하여 스레드 풀이 사용 가능한 스레드에 작업 항목을 할당하기 전에 대기하는 시간을 지정합니다. 지연은 TimeSpan 구조를 사용하여 지정됩니다.
참고 UI에 접근하고 작업 항목의 진행을 표시하기 위해 CoreDispatcher.RunAsync를 사용할 수 있습니다.
다음 예제에서는 3분 안에 실행되는 작업 항목을 만듭니다.
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 오버로드를 사용하여 추가 람다를 제공하세요. 타이머가 취소되거나 작업 항목이 완료될 때 실행됩니다.
다음 예제에서는 작업 항목을 제출하는 타이머를 만들고 작업 항목이 완료되거나 타이머가 취소될 때 메서드를 호출합니다.
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();
비고
UWP(유니버설 Windows 플랫폼) 앱은 UI 스레드를 차단할 수 있으므로 Thread.Sleep 을 사용할 수 없습니다. ThreadPoolTimer를 사용하여 작업 항목을 대신 만들 수 있으며, 이렇게 하면 UI 스레드를 차단하지 않고 작업 항목에서 수행하는 작업이 지연됩니다.
작업 항목, 타이머 작업 항목 및 정기적인 작업 항목을 보여 주는 전체 코드 샘플은 스레드 풀 샘플을 참조하세요. 코드 샘플은 원래 Windows 8.1용으로 작성되었지만 Windows 10에서 코드를 다시 사용할 수 있습니다.
반복 타이머에 대한 자세한 내용은 주기적 작업 항목 만들기를 참조하세요.
관련 항목
- 작업 항목을 스레드 풀에 제출
- 스레드 풀 사용하기 위한
모범 사례 - 타이머를 사용하여 작업 항목 제출