重要的应用程序接口(API)
了解如何创建定期重复的工作项。
创建定期工作项
使用 CreatePeriodicTimer 方法创建定期工作项。 请提供一个用于完成任务的 lambda,并使用 周期 参数来指定提交之间的间隔。 使用 TimeSpan 结构指定期间。 每次时间段结束时,都会重新提交工作项,因此请确保该时间段足够长,以便能够完成工作。
CreateTimer 返回 ThreadPoolTimer 对象。 如果计时器需要取消,请存储此对象。
注意 避免为间隔指定零值(或任何小于 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 重载,提供一个附加的 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();
注解
有关单用计时器的信息,请参阅 使用计时器提交工作项。
相关主题
- 将工作项提交到线程池
- 使用线程池 的最佳做法
- 使用计时器提交工作项