WaitForMultipleObjects - 2 timeouts?

Hi Experts,

I have the need for 2 separate timeouts as events coming back from WaitForMultipleObjects.  The normal timeout with WAIT_TIMEOUT is done.. How can I add a 2nd timeout event?  Perhaps a handle with some timer function that sets the event after a certain amount of time?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

A loop could do that, e.g.

DWORD adwTimeouts[] = {2000, 1000};
HANDLE ahWaitHandles = { handle1, handle2};

for (int i = 0; i <= 1; ++i) {

  if (WAIT_TIMEOUT != WaitForMultipleObjects(2,ahWaitHandles,FALSE,adwTimeouts[i])) {
    // not a timeout, do something

Open in new window

threadyAuthor Commented:
I don't think so- if timeout 2 is smaller, then it suffers... If I have one timeout of 10 seconds and the other is 30, I want the event to happen every 10 seconds and every 30 seconds...
Do the timeouts depend on which event fired? If so, you could use the event indicator that is returned to select the next timeout.

Alternatively, you could 'WaitForSingleObject()' in two separate threads...
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

threadyAuthor Commented:
Yeap... I guess that's the best I can do with this.  Not giving up yet though... :-)
So, what about distinguishing the events, e.g.

DWORD dwResult;
DWORD dwTimeout = 30000;
HANDLE ahWaitHandles = { handle1, handle2};

for (;;;) {

  if (WAIT_TIMEOUT != (dwResult = WaitForMultipleObjects(2,ahWaitHandles,FALSE,dwTimeout))) {
    // not a timeout, do something
    switch(dwResult - WAIT_OBJECT_0) {

      case 0: dwTimeout = 30000;  // 'handle1' fired

      case 0: dwTimeout = 20000;  // 'handle2' fired

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
As for the 'two thread solution' - think of it as

struct ThreadParam {
   HANDLE hObj;
  DWORD dwTimeout;

LONG WaitHandleThread(LPVOID pv) {

  ThreadParam* ptp = (ThreadParam*) pv;

  WaitForSingleObject(ptp->hObj, ptp->dwTimeout);


void WaitFor2Timeouts() {

  ThreadParam par1;
  par1.hObj = handle1;
  par1.dwTimeout = 20000;

  ThreadParam par2;
  par1.hObj = handle2;
  par1.dwTimeout = 30000;

  DWORD dwTID1, dwTID2;

  HANDLE hThr1 = CreateThread(NULL, 0, WaitHandleThread, (LPVOID) &par1, 0, &dwTID1);
  HANDLE hThr2 = CreateThread(NULL, 0, WaitHandleThread, (LPVOID) &par1, 0, &dwTID2);

  HANDLE ahWait[] = { hThr1, hThr2};

  WaitForMultipleObjects(2, ahWait, TRUE, INFINITE); // change that to 'FALSE' if that mathches more what you need

Open in new window

threadyAuthor Commented:
Thanks!  I'll be doing a hybrid of the "best solution".  There's one of the timeouts that is so unimportant and will be guaranteed to run less often than the other, so I'll just check if it's "time for that event too"... kinda thing...     :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.