+ Reply to Thread
Results 1 to 2 of 2

Thread: How to wake up my thread if it is already waiting for another event to get set?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    46

    How to wake up my thread if it is already waiting for another event to get set?

    I have following code:

    Code:
    UINT MyThread(LPVOID)
    {
          HANDLE hEvent = gEvent ; //this is global event handle 
    
          DWORD result = WaitForSingleObject(hEvent, INFINITE) ;
    
          switch(result)
          {
          case WAIT_OBJECT_0:
              {
                   /do something for this event.
              }
              break ;
          }
          return 0 ;
    }
    How to wake up above thread if it is already waiting for another event to get set?

    Please help...

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    38
    Use 'WaitForMultipleObjects' API for this.

    You can have more than one event object and your thread will wait for any of those event to get set.

    Once you have any event is set then you will come out of the wait state and can handle the scenario as below:

    Code:
    UINT MyThread(LPVOID)
    {
          HANDLE hEvents[2] = {hEvent1, hEvent2 } ;  //you should have access to hEvent1 and hEvent2 event handle objects!
          DWORD result = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE) ;
    
          switch(result)
          {
          case WAIT_OBJECT_0:
              {
                   /do something for this hEvent1.
              }
              break ;
         case WAIT_OBJECT_0 + 1:
              {
                   /do something for this hEvent2.
              }
              break ;
          }
          return 0 ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts