Aracılığıyla paylaş


Nasıl yapılır: Düzenli Aralıkla İleti Gönderme

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

Örnek

Aşağıdaki örnek, uzun bir timer işlem sırasında ilerleme durumunu raporlamak için bir nesnesi kullanır. Bu örnek, nesneyi concurrency::call nesnesine bağlartimer. call nesnesi, konsola düzenli aralıklarla bir ilerleme göstergesi yazdırır. concurrency::timer::start yöntemi zamanlayıcıyı ayrı bir bağlamda çalıştırır. İşlev, perform_lengthy_operation zaman alan bir işlemin benzetimini yapmak için ana bağlamda eşzamanlılık::wait işlevini çağırır.

// 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 çıkışı oluşturur:

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

Kod Derleniyor

Örnek kodu kopyalayıp bir Visual Studio projesine yapıştırın veya adlı report-progress.cpp bir dosyaya yapıştırın ve ardından bir Visual Studio Komut İstemi penceresinde aşağıdaki komutu çalıştırın.

cl.exe /EHsc report-progress.cpp

Ayrıca bkz.

Zaman Uyumsuz Aracılar Kitaplığı
Zaman Uyumsuz İleti Blokları
İleti Geçirme İşlevleri