Platform SDK: DLLs, Processes, and Threads |
The following example uses the CreateEvent function to create two event objects. It then uses the WaitForMultipleObjects function to wait for the state of one of the objects to be set to signaled.
HANDLE hEvents[2]; DWORD i, dwEvent; // Create two event objects. for (i = 0; i < 2; i++) { hEvents[i] = CreateEvent( NULL, // no security attributes FALSE, // auto-reset event object FALSE, // initial state is nonsignaled NULL); // unnamed object if (hEvents[i] == NULL) { printf("CreateEvent error: %d\n", GetLastError() ); ExitProcess(0); } } // The creating thread waits for other threads or processes // to signal the event objects. dwEvent = WaitForMultipleObjects( 2, // number of objects in array hEvents, // array of objects FALSE, // wait for any INFINITE); // indefinite wait // Return value indicates which event is signaled. switch (dwEvent) { // hEvent[0] was signaled. case WAIT_OBJECT_0 + 0: // Perform tasks required by this event. break; // hEvent[1] was signaled. case WAIT_OBJECT_0 + 1: // Perform tasks required by this event. break; // Return value is invalid. default: printf("Wait error: %d\n", GetLastError()); ExitProcess(0); }