定期发送消息

摘自 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值