Search code examples
c++multithreadingeventsvisual-c++winapi

Will using CreateEvent to create/open an event that already exists reset the signal?


If I use CreateEvent to open an event:

 responseWaitEvent = CreateEvent(NULL,        // no security
  TRUE,       // manual-reset event
  FALSE,      // not signaled
  (LPTSTR)eventName); // event name

And this event already exists and has been signaled. Will this call reset the signal (because of setting initial state to FALSE)?

Or will it immediately exit the wait when called:

 if (responseWaitEvent)
 {
  DWORD dw = WaitForSingleObjectEx(responseWaitEvent, m_ResponseTimeoutMillis, FALSE);

Solution

  • The second call will open the event, it will not change the event's state.

    I know this because the documentation says that it opens it but not that it resets it. If it reset it, that would be important enough to mention.

    HANDLE responseWaitEvent = CreateEvent(NULL,        // no security
        TRUE,       // manual-reset event
        TRUE,      // signaled
        (LPTSTR)_T("TESTEvent")); // event name
        assert(responseWaitEvent && "Could not create event");
    
    responseWaitEvent = CreateEvent(NULL,        // no security
        TRUE,       // manual-reset event
        FALSE,      // not signaled
        (LPTSTR)_T("TESTEvent")); // event name
    
        assert(responseWaitEvent && "Could not open event");
    
    DWORD dw = WaitForSingleObjectEx(responseWaitEvent, 10000, FALSE);
    assert(dw != WAIT_TIMEOUT);
    

    And the opposite:

    HANDLE responseWaitEvent2 = CreateEvent(NULL,        // no security
        TRUE,       // manual-reset event
        FALSE,      // not signaled
        (LPTSTR)_T("TESTEvent2")); // event name
        assert(responseWaitEvent && "Could not create event");
    
    responseWaitEvent2 = CreateEvent(NULL,        // no security
        TRUE,       // manual-reset event
        TRUE,      // signaled
        (LPTSTR)_T("TESTEvent2")); // event name
        assert(responseWaitEvent && "Could not open event");
    
    DWORD dw2 = WaitForSingleObjectEx(responseWaitEvent2, 10000, FALSE);
    assert(dw2 == WAIT_TIMEOUT);