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?

Thanks!
Mike
LVL 1
threadyAsked:
Who is Participating?
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.

jkrCommented:
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

0
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...
0
jkrCommented:
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...
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

threadyAuthor Commented:
Yeap... I guess that's the best I can do with this.  Not giving up yet though... :-)
0
jkrCommented:
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
      break;

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

Open in new window

0

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
jkrCommented:
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);

  return0;
}

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

0
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...     :-)
0
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
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.