Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Waiting for Multiple Objects

The following example uses the CreateEvent function to create two event objects and the CreateThread function to create a thread. It then uses the WaitForMultipleObjects function to wait for the thread to set the state of one of the objects to signaled using the SetEvent function.

For an example that waits for a single object, see Using Mutex Objects.

#include <windows.h>
#include <stdio.h>

HANDLE ghEvents[2];


int main( void )
    HANDLE hThread; 
    DWORD i, dwEvent, dwThreadID; 

    // Create two event objects

    for (i = 0; i < 2; i++) 
        ghEvents[i] = CreateEvent( 
            NULL,   // default security attributes
            FALSE,  // auto-reset event object
            FALSE,  // initial state is nonsignaled
            NULL);  // unnamed object

        if (ghEvents[i] == NULL) 
            printf("CreateEvent error: %d\n", GetLastError() ); 

    // Create a thread

    hThread = CreateThread( 
                 NULL,         // default security attributes
                 0,            // default stack size
                 (LPTHREAD_START_ROUTINE) ThreadProc, 
                 NULL,         // no thread function arguments
                 0,            // default creation flags
                 &dwThreadID); // receive thread identifier

    if( hThread == NULL )
        printf("CreateThread error: %d\n", GetLastError());
        return 1;

    // Wait for the thread to signal one of the event objects

    dwEvent = WaitForMultipleObjects( 
        2,           // number of objects in array
        ghEvents,     // array of objects
        FALSE,       // wait for any object
        5000);       // five-second wait

    // The return value indicates which event is signaled

    switch (dwEvent) 
        // ghEvents[0] was signaled
        case WAIT_OBJECT_0 + 0: 
            // TODO: Perform tasks required by this event
            printf("First event was signaled.\n");

        // ghEvents[1] was signaled
        case WAIT_OBJECT_0 + 1: 
            // TODO: Perform tasks required by this event
            printf("Second event was signaled.\n");

        case WAIT_TIMEOUT:
            printf("Wait timed out.\n");

        // Return value is invalid.
            printf("Wait error: %d\n", GetLastError()); 

    // Close event handles

    for (i = 0; i < 2; i++) 
    return 0;   

DWORD WINAPI ThreadProc( LPVOID lpParam )

    // lpParam not used in this example

    // Set one event to the signaled state

    if ( !SetEvent(ghEvents[0]) ) 
        printf("SetEvent failed (%d)\n", GetLastError());
        return 1;
    return 0;



Community Additions

© 2015 Microsoft