+ Reply to Thread
Results 1 to 2 of 2

Thread: Using Event in WIN32?

  1. #1

    Using Event in WIN32?

    Using Event in WIN32?

    Please provide example.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Event is used a thread synchronization object.

    It can also be used for signaling purpose.

    Event can be used in same process or across the processes.

    Example:

    Code:
    UINT ThreadFunc(LPVOID lpVoid)
    {
    	//Do some task in this thread....
    	HANDLE hEvent = (LPVOID) lpVoid ;
    
    	SetEvent(hEvent) ;
    
    	AfxEndThread(0, TRUE) ;
    
    	return 0 ;
    }
    
    void CreateAnEventAndWaitForIt()
    {
    	HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL) ;
    
    	if (NULL != hEvent)
    	{
    		CWinThread *pWinThread = AfxBeginThread(ThreadFunc, (LPVOID) hEvent) ;
    
    		DWORD dwResult = WaitForSingleObject(hEvent, INFINITE) ;
    
    		switch (dwResult)
    		{
    		case WAIT_OBJECT_0:
    			printf("Event has been set!") ;
    			break ;
    		default:
    			break ;
    		}
    
    		//this will come out of wait immediately as event is already set!
    		dwResult = WaitForSingleObject(hEvent, INFINITE) ;
    
    		switch (dwResult)
    		{
    		case WAIT_OBJECT_0:
    			printf("Event has been set!") ;
    			break ;
    		default:
    			break ;
    		}
    
    		//reset the event
    		ResetEvent(hEvent) ;
    
    		//this will ***not*** come out of wait as event is in reset state!
    		dwResult = WaitForSingleObject(hEvent, INFINITE) ;
    
    	}
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts