Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Send a Message at a Regular Interval

This example shows how to use the Concurrency::timer class to send a message at a regular interval.

The following example uses a timer object to report progress during a lengthy operation. This example links the timer object to a Concurrency::call object. The call object prints a progress indicator to the console at a regular interval. The Concurrency::timer::start method runs the timer on a separate context. The perform_lengthy_operation function calls the Concurrency::wait function on the main context to simulate a time-consuming operation.

// 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.

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.

   // Perform a lengthy operation on the main context.

   // Stop the timer and print a message.

   wcout << L"done.";

This example produces the following sample output:

Performing a lengthy operation..........done.

Copy the example code and paste it in a Visual Studio project, or paste it in a file that is named report-progress.cpp and then run the following command in a Visual Studio 2010 Command Prompt window.

cl.exe /EHsc report-progress.cpp

Community Additions

© 2016 Microsoft