2012-06-15 25 views
1

如果我正在使用WaitForMultipleObjects,並且該函數返回WAIT_TIMEOUT,那麼如何獲取哪個對象或對象導致超時發生?如何在使用WaitForMultipleObjects時獲取哪個對象超時?

另一個問題我已經是如果多個對象發出信號,因爲返回值只返回的第一個對象,它作爲檢測信號,如何獲取這些信號中的其他對象?

#include <windows.h> 
#include <stdio.h> 

HANDLE ghEvents[2]; 

DWORD WINAPI ThreadProc(LPVOID); 

int main(void) 
{ 
    HANDLE hThread; 
    DWORD i, dwEvent, dwThreadID; 

    // Create two event objects 

    for (i = 0; i < 2; i++) 
    { 
     ghEvents[i] = CreateEvent( 
      NULL, // default security attributes 
      FALSE, // auto-reset event object 
      FALSE, // initial state is nonsignaled 
      NULL); // unnamed object 

     if (ghEvents[i] == NULL) 
     { 
      printf("CreateEvent error: %d\n", GetLastError()); 
      ExitProcess(0); 
     } 
    } 

    // Create a thread 

    hThread = CreateThread( 
       NULL,   // default security attributes 
       0,   // default stack size 
       (LPTHREAD_START_ROUTINE) ThreadProc, 
       NULL,   // no thread function arguments 
       0,   // default creation flags 
       &dwThreadID); // receive thread identifier 

    if(hThread == NULL) 
    { 
     printf("CreateThread error: %d\n", GetLastError()); 
     return 1; 
    } 

    // Wait for the thread to signal one of the event objects 

    dwEvent = WaitForMultipleObjects( 
     2,   // number of objects in array 
     ghEvents,  // array of objects 
     FALSE,  // wait for any object 
     5000);  // five-second wait 

    // The return value indicates which event is signaled 

    switch (dwEvent) 
    { 
     // ghEvents[0] was signaled 
     case WAIT_OBJECT_0 + 0: 
      // TODO: Perform tasks required by this event 
      printf("First event was signaled.\n"); 
      break; 

     // ghEvents[1] was signaled 
     case WAIT_OBJECT_0 + 1: 
      // TODO: Perform tasks required by this event 
      printf("Second event was signaled.\n"); 
      break; 

     case WAIT_TIMEOUT: 
      // How can I get which object timed out? 
      printf("Wait timed out.\n"); 
      break; 

     // Return value is invalid. 
     default: 
      printf("Wait error: %d\n", GetLastError()); 
      ExitProcess(0); 
    } 

    // Close event handles 

    for (i = 0; i < 2; i++) 
     CloseHandle(ghEvents[i]); 

    return 0; 
} 

DWORD WINAPI ThreadProc(LPVOID lpParam) 
{ 

    // lpParam not used in this example 
    UNREFERENCED_PARAMETER(lpParam); 

    // Set one event to the signaled state 

    if (!SetEvent(ghEvents[0])) 
    { 
     printf("SetEvent failed (%d)\n", GetLastError()); 
     return 1; 
    } 
    return 0; 
} 
+2

如果出現超時,是不是意味着你等待的對象沒有回答? – Eregrith

+1

對於你的第二個問題,如果有一個對象發出信號,我想你必須重新呼叫你的等待。 – Eregrith

回答

8

WaitForMultipleObjects(...)回報與WAIT_TIMEOUT返回碼,則表明沒有你,你,你的對象等待給定的時間量內信號。

功能基本您指定超時,只有更早返回,如果可等待對象的人會在該時間之前信號的時間。這意味着WAIT_TIMEOUT返回代碼與您等待的任何對象都沒有關聯。

你的第二個問題的部分由Eregriths評論回答。要檢查是否有其他對象也有信號,可以再次撥打WaitForMultipleObjects(...),根據您的需要,將超時值設置爲0(不要等待)。當WaitForMultipleObjects(...)回報與WAIT_TIMEOUT你知道,沒有其他對象是在一個信號狀態,在你打電話的時候,但你應該記住,該對象,導致你的第一個調用返回可能被再次通知。所以你可以從數組中排除它,或者簡單地用WaitForSingleObject(...)函數檢查單個對象的狀態。

如果你想確保所有對象都暗示,你也可以用bWaitAll參數玩。如果所有的對象都在信號狀態WaitForMultipleObjects(...)然後將只返回,

希望有點幫助。

+0

哦,沒有看到超時被指定爲數組而不是單個元素,我的不好,第二個問題呢? – shawn

+0

@shawn:查看我更新的答案。 –

相關問題