DWORD WINAPI WaitForMultipleObjects( _In_ DWORD nCount, _In_reads_(nCount) CONST HANDLE *lpHandles, _In_ BOOL bWaitAll, _In_ DWORD dwMilliseconds );nCount 句柄的数量 最大值为MAXIMUM_WAIT_OBJECTS(64)
举例:
#include <iostream> #include <Windows.h> using namespace std; int main() { //创建3个Event对象 HANDLE hEvent1 = CreateEvent(NULL, // 安全属性 FALSE, // 是否手动重置对象,为TRUE时等待事件后需手动重置,否则一直处于激发状态 NULL, // 是否初始化为激发状态 NULL // 名称 ); HANDLE hEvent2 = CreateEvent(NULL, TRUE, NULL, NULL); HANDLE hEvent3 = CreateEvent(NULL, FALSE, NULL, NULL); HANDLE hObject[3]; hObject[0] = hEvent1; hObject[1] = hEvent2; hObject[2] = hEvent3; /// 激活所有对象 SetEvent(hEvent1); SetEvent(hEvent2); SetEvent(hEvent3); /// 等待HANDLE数组中的所有对象,只有所有对象都被激活时才会返回 DWORD dwResult = WaitForMultipleObjects(3, hObject, TRUE, INFINITE); switch (dwResult) { case WAIT_OBJECT_0: cout << "所有对象都被激活" << endl; break; case WAIT_FAILED: cout << "等待失败" << endl; break; default: break; } /// 等待成功时,自动重置Event对象会重置Event状态,手动重置则不会,如此时hEvent1、hEvent3已被重置为未激发状态,hEvent2仍处于激发状态 /// 此时hEvent2已被重置,再次激活 SetEvent(hEvent3); /// 等待多个对象中的某一个时,是按数组由前到后遍历,因此Event2、Event3均被激活的情况下,等待到的事件为Event2 dwResult = WaitForMultipleObjects(3, hObject, FALSE, INFINITE); switch (dwResult) { case WAIT_OBJECT_0: cout << "hEvent1被激活" << endl; break; case WAIT_OBJECT_0 + 1: cout << "hEvent2被激活" << endl; break; case WAIT_OBJECT_0 + 2: cout << "hEvent3被激活" << endl; break; case WAIT_FAILED: cout << "等待失败" << endl; break; default: break; } /// 手动重置Event对象 ResetEvent(hEvent2); /// 再次等待,Event2处于未激发状态,等待到的事件为Event3 dwResult = WaitForMultipleObjects(3, hObject, FALSE, INFINITE); switch (dwResult) { case WAIT_OBJECT_0: cout << "hEvent1被激活" << endl; break; case WAIT_OBJECT_0 + 1: cout << "hEvent2被激活" << endl; break; case WAIT_OBJECT_0 + 2: cout << "hEvent3被激活" << endl; break; case WAIT_FAILED: cout << "等待失败" << endl; break; default: break; } /// 释放内核对象 CloseHandle(hEvent1); CloseHandle(hEvent2); CloseHandle(hEvent3); system("pause"); return 0; }