wait_events(int timeout_ms) { if (_handles.empty()) { if (WaitMessage()) { return process_system_events(); } else { THROW("wait failed %d", GetLastError()); } } DWORD wait_res = MsgWaitForMultipleObjectsEx(_handles.size(), &_handles[0], timeout_ms, QS_ALLINPUT, 0); if (wait_res == WAIT_TIMEOUT) { return false; } if (wait_res == WAIT_FAILED) { THROW("wait failed %d", GetLastError()); } int event_index = wait_res - WAIT_OBJECT_0; if (event_index == _handles.size()) { return process_system_events(); } else if ((event_index >= 0) && (event_index < (int)_handles.size())) { _events[event_index]->action(); return false; } else { THROW("invalid event id"); } }