Aracılığıyla paylaş


CEvent sınıfı

Başka bir olay oluştuğunu bildirmek bir iş parçacığını etkinleştirir bir eşitleme nesnesi olan bir olayı temsil eder.

class CEvent : public CSyncObject

Üyeler

efk30beh.collapse_all(tr-tr,VS.110).gifOrtak Oluşturucusu

Ad

Description

CEvent::CEvent

Yapıları bir CEvent nesnesi.

efk30beh.collapse_all(tr-tr,VS.110).gifPublic yöntemler

Ad

Description

CEvent::PulseEvent

Kümeleri kullanılabilir olaya (erdiği), bekleyen iş parçacığı bırakır ve olay için kullanılamaz (nonsignaled) ayarlar.

CEvent::ResetEvent

Olay için kullanılamaz (nonsignaled) ayarlar.

CEvent::SetEvent

Herhangi bir bekleyen iş parçacığı bırakır ve olay için kullanılabilir (sinyal verilmiş) ayarlar.

CEvent::Unlock

Olay nesnesi serbest bırakır.

Notlar

Bir iş parçacığı olduğunda, görevini gerçekleştirmesi bilmeniz gereken, olayları faydalıdır.Örneğin, yeni veriler kullanılabilir duruma geldiğinde bir veri arşiv verilerinin kopyasını bir iş parçacığı bildirilmelidir.Kullanarak bir CEvent yeni veri bulunduğunda, kopya iş parçacığı bildirmek için nesnenin iş parçacığı mümkün olan en kısa sürede kendi görevi gerçekleştirebilirsiniz.

CEventnesnelerin iki türü vardır: el ile ve otomatik.

Otomatik CEvent nesnesinin en az bir iş parçacığı yayımlandıktan sonra erdiği bir (kullanılamaz) durumu otomatik olarak döndürür.Varsayılan olarak, bir CEvent nesne geçirmek sürece otomatik TRUE için bManualReset yapım sırasında parametresi.

El CEvent nesne ayarlanmış durumda kalır SetEvent veya ResetEvent kadar işlevi çağrılır.El ile oluşturmak için CEvent nesne, pass TRUE için bManualReset yapım sırasında parametresi.

Kullanmak için bir CEvent nesne, değiştirmezler CEvent nesne gerekli olduğunda.Bekleyin ve de uygulamanız başlangıçta kendisine ait olduğunu belirtmek için istediğiniz olayın adını belirtin.Daha sonra Olay kurucu döndüğünde erişebilirsiniz.Call SetEvent sinyal (kullanılabilir duruma) için olay nesnesi ve ardından arama Unlock bitince kontrollü kaynağına erişim.

Alternatif bir yöntem kullanmak için CEvent nesnedir türünde bir değişken eklemek için CEvent denetlemek istediğiniz sınıfı için veri üyesi olarak.Denetlenen Nesne oluşturma sırasında çağırdığınız CEvent veri üyesi olup olay başlangıçta erdiği ve ayrıca tanımlamak istediğiniz olay nesnesinin olay (işlem sınırları boyunca kullanılacak olan varsa) adını türünü belirtin ve tüm güvenlik özelliklerini istediğiniz.

Tarafından denetlenen bir kaynağa erişmek için bir CEvent nesne bu yolla, ilk iki türde bir değişken oluşturmak CSingleLock veya CMultiLock erişim yöntemi, kaynak.Ardından çağrı Lock kilit nesnesinin yöntemi (örneğin, CMultiLock::Lock).Bu noktada, kendi iş parçacığı ya da kaynak, kaynak serbest bırakılması ve erişmek veya kaynağın serbest bırakılması bekle, zaman aşımına erişebilir ve kaynağa erişmek başarısız.Her durumda, kaynak, iş parçacığı için güvenli bir biçimde erişilmedi.Kaynağı serbest bırakmak için çağrı SetEvent olay nesnesi sinyal ve daha sonra kullanmak için Unlock kilit nesnesinin yöntemi (örneğin, CMultiLock::Unlock), veya kilit Nesne kapsamı dışında kalan olanak sağlar.

Nasıl kullanılacağı hakkında daha fazla bilgi için CEvent nesneleri Bkz: Çoklu İş Parçacığı Kullanımı: Eşitleme Sınıflarını Kullanma.

Örnek

// The following demonstrates trivial usage of the CEvent class.
// A CEvent object is created and passed as a parameter to another 
// thread.  The other thread will wait for the event to be signaled
// and then exit

UINT __cdecl MyThreadProc(LPVOID lpParameter)
{
   CEvent* pEvent = (CEvent*)(lpParameter);
   VERIFY(pEvent != NULL);

   // Wait for the event to be signaled
   ::WaitForSingleObject(pEvent->m_hObject, INFINITE);

   // Terminate the thread
   ::AfxEndThread(0, FALSE); 
   return 0L;
}

void CEvent_Test()
{
   // Create the CEvent object that will be passed to the thread routine
   CEvent* pEvent = new CEvent(FALSE, FALSE);

   // Create a thread that will wait on the event
   CWinThread* pThread;
   pThread = ::AfxBeginThread(&MyThreadProc, pEvent, 0, 0, CREATE_SUSPENDED, NULL);
   pThread->m_bAutoDelete = FALSE; 
   pThread->ResumeThread();

   // Signal the thread to do the next work item
   pEvent->SetEvent();

   // Wait for the thread to consume the event and return
   ::WaitForSingleObject(pThread->m_hThread, INFINITE); 
   delete pThread;
   delete pEvent;
}
// This example builds upon the previous one.
// A second thread is created to calculate prime numbers.
// The main thread will signal the second thread to calulate the next 
// prime number in the series.  The second thread signals the first 
// after each number is calculated. Finally, after several iterations 
// the worker thread is signaled to terminate.

class CPrimeTest
{
public:
   CPrimeTest()
      : m_pCalcNext(new CEvent(FALSE, FALSE))
      , m_pCalcFinished(new CEvent(FALSE, FALSE))
      , m_pTerminateThread(new CEvent(FALSE, FALSE))
      , m_iCurrentPrime(0)
   {   
      // Create a thread that will calculate the prime numbers
      CWinThread* pThread;
      pThread = ::AfxBeginThread(&PrimeCalcProc, this, 0, 0, CREATE_SUSPENDED, NULL);
      pThread->m_bAutoDelete = FALSE; 
      pThread->ResumeThread();

      // Calcuate the first 10 prime numbers in the series on the thread
      for(UINT i = 0; i < 10; i++)
      {
         // Signal the thread to do the next work item
         m_pCalcNext->SetEvent();
         // Wait for the thread to complete the current task
         ::WaitForSingleObject(m_pCalcFinished->m_hObject, INFINITE);
         // Print the result
         TRACE(_T("The value of m_iCurrentPrime is: %d\n"), m_iCurrentPrime);
      }

      // Notify the worker thread to exit and wait for it to complete
      m_pTerminateThread->SetEvent();
      ::WaitForSingleObject(pThread->m_hThread, INFINITE); 
      delete pThread;
   }
   ~CPrimeTest()
   {
      delete m_pCalcNext;
      delete m_pCalcFinished;
      delete m_pTerminateThread;
   }

private:
   // Determines whether the given number is a prime number
   static BOOL IsPrime(INT ThisPrime)
   {
      if(ThisPrime < 2) 
         return FALSE;

      for(INT n = 2; n < ThisPrime; n++)
      {
         if(ThisPrime % n == 0)
            return FALSE;
      }
      return TRUE;
   }

   // Calculates the next prime number in the series
   static INT NextPrime(INT ThisPrime)
   {
      while(TRUE)
      {
         if(IsPrime(++ThisPrime))
         {
            return ThisPrime;
         }
      }
   }

   // Worker thread responsible for calculating the next prime
   // number in the series
   static UINT __cdecl PrimeCalcProc(LPVOID lpParameter)
   {
      CPrimeTest* pThis = static_cast<CPrimeTest*>(lpParameter);
      VERIFY(pThis != NULL);

      VERIFY(pThis->m_pCalcNext != NULL);
      VERIFY(pThis->m_pCalcFinished != NULL);
      VERIFY(pThis->m_pTerminateThread != NULL);

      // Create a CMultiLock object to wait on the various events
      // WAIT_OBJECT_0 refers to the first event in the array, WAIT_OBJECT_0+1 refers to the second
      CSyncObject* pWaitObjects[] = { pThis->m_pCalcNext, pThis->m_pTerminateThread };
      CMultiLock MultiLock(pWaitObjects, 2L);
      while(MultiLock.Lock(INFINITE, FALSE) == WAIT_OBJECT_0) 
      {         
         // Calculate next prime
         pThis->m_iCurrentPrime = NextPrime(pThis->m_iCurrentPrime);
         // Notify main thread calculation is complete
         pThis->m_pCalcFinished->SetEvent();
       } 

      // Terminate the thread
       ::AfxEndThread(0, FALSE); 
      return 0L;
   }

   CEvent* m_pCalcNext;      // notifies worker thread to calculate next prime
   CEvent* m_pCalcFinished;   // notifies main thread current calculation is complete
   CEvent* m_pTerminateThread;   // notifies worker thread to terminate

   INT m_iCurrentPrime;   // current calculated prime number
};

Devralma hiyerarşisi

CObject

CSyncObject

CEvent

Gereksinimler

Başlık: afxmt.h

Ayrıca bkz.

Başvuru

CSyncObject sınıfı

Hiyerarşi grafik