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

이 예제에서는 동시성::timer 클래스 를 사용하여 일정한 간격으로 메시지를 보내는 방법을 보여줍니다.

예시

다음 예제에서는 긴 작업 중에 개체를 사용하여 timer 진행률을 보고합니다. 다음은 개체를 timer 동시성::call 개체에 연결하는 예제입니다. 개체는 call 정기적으로 진행률 표시기를 콘솔에 출력합니다. 동시성::timer::start 메서드는 별도의 컨텍스트에서 타이머를 실행합니다. 이 함수는 perform_lengthy_operation 기본 컨텍스트에서 동시성::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 프로젝트에 붙여넣거나 이름이 지정된 report-progress.cpp 파일에 붙여넣은 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행합니다.

cl.exe /EHsc report-progress.cpp

참고 항목

비동기 에이전트 라이브러리
비동기 메시지 블록
메시지 전달 함수