سؤال

It appears that sometimes WaitForSingleObject will wait for timeout even though the Event has been signalled.

The signalling thread receives success from SetEvent() every time. However, the thread being signalled won't always hear the signal. If I add a redundant SetEvent() to the signalling thread the issue will go away.

Obviously something is amiss here. What are the possible causes?

// Signalling Thread
if (SetEvent(waitEvent))
    consoleprintf(L"\r\nEvent Set.");

And the should wait for a delay time or gets signalled. But doesn't always get signalled.

waitEvent = CreateEvent( 
    NULL,               // default security attributes
    FALSE,               // auto-reset event
    FALSE,              // initial state is nonsignaled
    TEXT("WaitEvent")  // object name
    ); 

for(;;)
{
   dwWaitResult = WaitForSingleObject(waitEvent, // event handle
        5000);    

  switch (dwWaitResult) 
  {
    // Event object was signaled
    case WAIT_OBJECT_0:
        consoleprintf(L"\r\nSuccess.");
        break; 

    case WAIT_FAILED:
    //break;
    case WAIT_ABANDONED:
        consoleprintf(L"\r\nWait failed.");
        break;
    case WAIT_TIMEOUT:
        consoleprintf(L"\r\nWait Timed out: %d", waitTime);
        break;
          default: 
        break;
   }
}
هل كانت مفيدة؟

المحلول

Since you are creating a named event, the event is accessible to other processes which know the name. That other process may be waiting on the event as well, causing it to eat the signal. Unless there is some reason that the event needs to be visible to other processes, you should pass NULL as the event name. That creates an anonymous event which other processes cannot access.

Note that if give the event a name, then you will have the problem that if two copies of your program are running, they will both be using the same event, which will create this same problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top