摘自 http://msdn.microsoft.com/zh-cn/library/dd728065(v=vs.110).aspx
本示例演示如何使用 concurrency::timer 定期时间间隔发送消息的类。
下面的示例使用 timer 对象报告长时间操作的进度。 此示例的链接timer对象的 concurrency::call 对象。 call 对象定期将进度指示器输出到控制台。Concurrency::timer::start 方法上不同于运行计时器。 perform_lengthy_operation函数调用 concurrency::wait 上主上下文来模拟需要较长时间的函数。
// report-progress.cpp // compile with: /EHsc #include <agents.h> #include <iostream> using namespace concurrency; using namespace std; // Simulates a lengthy operation. void perform_lengthy_operation() { // Yield the current context for one second. wait(1000); } int wmain() { // Create a call object that prints a single character to the console. call<wchar_t> report_progress([](wchar_t c) { wcout << c; }); // Create a timer object that sends the dot character to the // call object every 100 milliseconds. timer<wchar_t> progress_timer(100, L'.', &report_progress, true); wcout << L"Performing a lengthy operation"; // Start the timer on a separate context. progress_timer.start(); // Perform a lengthy operation on the main context. perform_lengthy_operation(); // Stop the timer and print a message. progress_timer.stop(); wcout << L"done."; }
此示例产生下面的示例输出:
Performing a lengthy operation..........done.