定期发送消息

来源:互联网 发布:js 展开折叠 section 编辑:程序博客网 时间:2024/06/07 01:04

摘自 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 上主上下文来模拟需要较长时间的函数。

C++
// 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.
编译代码

将示例代码复制并将其粘贴在 Visual Studio 项目中,或将它粘贴到一个文件,名为报告 progress.cpp ,然后在 Visual Studio 命令提示符窗口中运行以下命令。

cl.exe /EHsc report-progress.cpp


0 0