知了博客

集天地之精华,吸日月之灵气

« DirectShow编程(3.2)- 关于DirectShow - Filter Graph和它的组件FM 中的 RDS 功能 »

win32 api Event

 include "stdafx.h"   
 include "MyEvent.h"   
    
 ifdef _DEBUG   
 define new DEBUG_NEW   
 undef THIS_FILE   
 static char THIS_FILE[] = __FILE__;   
 endif   
    
 /////////////////////////////////////////////////////////////////////////////   
 // The one and only application object   
    
 CWinApp theApp;   
    
 using namespace std;   
    
 DWORD threadA(void* pD)   
 {   
     int iID=(int)pD;   
     //ÔÚÄÚ²¿ÖØдò¿ª   
     HANDLE hCounterIn=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try");   
    
     printf("\tthread %d begin\n",iID);   
     //ÉèÖóÉΪÓÐÐźÅ״̬   
     Sleep(1000);   
     SetEvent(hCounterIn);   
     Sleep(1000);   
     printf("\tthread %d end\n",iID);   
     CloseHandle(hCounterIn);   
     return 0;   
 }   
    
 DWORD threadB(void* pD)   
 {   
     //µÈ´ýthreadA½áÊøºóÔÙ¼ÌÐøÖ´ÐР  
     int iID=(int)pD;   
     //ÔÚÄÚ²¿ÖØдò¿ª   
     HANDLE hCounterIn=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try");   
    
     if(WAIT_TIMEOUT == WaitForSingleObject(hCounterIn,10*1000))   
     {   
         printf("\t\tthread %d wait time out\n",iID);   
     }   
     else   
     {   
         printf("\t\tthread %d wait ok\n",iID);   
     }   
     CloseHandle(hCounterIn);   
     return 0;   
 }   
    
 void main()   
 {   
     HANDLE hCounter=NULL;   
     if( (hCounter=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try"))==NULL)   
     {   
         //Èç¹ûûÓÐÆäËû½ø³Ì´´½¨Õâ¸öʼþ£¬ÔòÖØд´½¨£¬¸ÃʼþΪÈ˹¤ÖØÖÃʼþ   
         hCounter = CreateEvent(NULL,TRUE,FALSE,"My Try");   
     }   
    
     //´´½¨Ï̠߳  
     HANDLE hThread[3];   
     printf("test of manual rest event\n");   
     CWinThread* pT1=AfxBeginThread((AFX_THREADPROC)threadA,(void*)1);   
     CWinThread* pT2=AfxBeginThread((AFX_THREADPROC)threadB,(void*)2);   
     CWinThread* pT3=AfxBeginThread((AFX_THREADPROC)threadB,(void*)3);   
     hThread[0]=pT1->m_hThread;   
     hThread[1]=pT2->m_hThread;   
     hThread[2]=pT3->m_hThread;   
     //µÈ´ýÏ߳̽áÊø   
     WaitForMultipleObjects(3,hThread,TRUE,INFINITE);   
     //¹Ø±Õ¾ä±ú   
     CloseHandle(hCounter);   
    
     if( (hCounter=OpenEvent(EVENT_ALL_ACCESS,FALSE,"My Try"))==NULL)   
     {   
         //Èç¹ûûÓÐÆäËû½ø³Ì´´½¨Õâ¸öʼþ£¬ÔòÖØд´½¨£¬¸ÃʼþΪ×Ô¶¯ÖØÖÃʼþ   
         hCounter = CreateEvent(NULL,FALSE,FALSE,"My Try");   
     }   
     //´´½¨Ï̠߳  
     printf("test of auto rest event\n");   
     pT1=AfxBeginThread((AFX_THREADPROC)threadA,(void*)1);   
     pT2=AfxBeginThread((AFX_THREADPROC)threadB,(void*)2);   
     pT3=AfxBeginThread((AFX_THREADPROC)threadB,(void*)3);   
     hThread[0]=pT1->m_hThread;   
     hThread[1]=pT2->m_hThread;   
     hThread[2]=pT3->m_hThread;   
     //µÈ´ýÏ߳̽áÊø   
     WaitForMultipleObjects(3,hThread,TRUE,INFINITE);   
     //¹Ø±Õ¾ä±ú   
     CloseHandle(hCounter);   
 }  


来源:PUDN

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619 Code detection by Codefense  theme by BokeZhuti

Copyright know blog. Some Rights Reserved.站长(msn):webmaster#webgou.info(#换成@) 粤ICP备09183716号