重要的应用程序接口(API)
了解如何创建一个在计时器结束后执行的工作项。
创建单次计时器
使用 CreateTimer 方法为工作项创建计时器。 提供一个执行任务的 lambda 函数,并使用 延迟 参数来指定线程池在可以将工作项分配给可用线程之前的等待时间。 使用 TimeSpan 结构指定延迟。
注意 您可以使用 CoreDispatcher.RunAsync 来访问 UI 并显示工作项的进度。
以下示例创建一个工作项,该工作项在三分钟内运行:
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 重载来提供额外的 lambda。 当工作项完成或计时器被取消时,程序将运行。
以下示例创建一个计时器,该计时器提交工作项,并在工作项完成或计时器取消时调用方法:
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.
}
}));
}));
取消计时器
如果计时器仍在倒计时,但不再需要工作项,请调用 取消。 计时器已取消,工作项不会提交到线程池。
DelayTimer.Cancel();
DelayTimer->Cancel();
注解
通用 Windows 平台 (UWP) 应用无法使用 Thread.Sleep,因为它可以阻止 UI 线程。 可以使用 ThreadPoolTimer 来创建工作项,这将延迟工作项完成的任务,而不会阻止 UI 线程。
有关演示工作项、计时器工作项和定期工作项的完整代码示例,请参阅 线程池示例。 该代码示例最初是为 Windows 8.1 编写的,但代码可以在 Windows 10 中重新使用。
关于重复计时器的详细信息,请参阅 创建定期工作项。
相关主题
- 将工作项提交到线程池
- 使用线程池 的最佳做法
- 使用计时器提交工作项