방법: 정기적으로 메시지 보내기

사용 하는 방법을 보여 주는이 예제는 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.

코드 컴파일

예제 코드를 복사 하 고 Visual Studio 프로젝트에 붙여 또는 라는 파일에 붙여 보고서 progress.cpp 및 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행 합니다.

cl.exe /EHsc report-progress.cpp

참고 항목

참조

timer 클래스

개념

비동기 에이전트 라이브러리

비동기 메시지 블록

메시지 전달 함수