Aracılığıyla paylaş


Nasıl yapılır: düzenli aralıklarla ileti gönderme

Bu örnek, nasıl kullanılacağını gösterir concurrency::timer düzenli aralıklarla ileti göndermek için sınıf.

Örnek

Aşağıdaki örnek bir timer uzun bir işlem sırasında ilerlemesini raporlamak için nesne.Bu örnek bağlantılar timer nesneyi bir concurrency::call nesnesi.call Nesne düzenli aralıklarla bir ilerleme göstergesi konsola yazdırır.Concurrency::timer::start yöntemi timer ayrı bir içerik üzerinde çalışır.perform_lengthy_operation İşlev çağrıları concurrency::wait indirilir benzetimini yapmak için ana içerik üzerinde işlevi.

// 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.";
}

Bu örnek, aşağıdaki örnek çıktı üretir:

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

Kod Derleniyor

Örnek kodu kopyalayın ve Visual Studio Project'te yapıştırın veya adlı bir dosyaya yapıştırın rapor progress.cpp ve Visual Studio komut istemi penceresinde aşağıdaki komutu çalıştırın.

cl.exe /EHsc report-progress.cpp

Ayrıca bkz.

Başvuru

Timer sınıfı

Kavramlar

Zaman uyumsuz aracıları kitaplığı

Zaman uyumsuz ileti blokları

İleti gönderme fonksiyonları