Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4623680
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:01:25+00:00 2026-05-22T03:01:25+00:00

I have an auto-reset event object created like this: handle = CreateEvent(NULL, true, false,

  • 0

I have an auto-reset event object created like this:

handle = CreateEvent(NULL, true, false, NULL);

…and (for some unit testing purposes) I want to check if it’s signalled at a certain point. I’m aware of the ‘correct’ way of using events – this is purely for a diagnostic harness.

For a manual reset event I can just use…

bool signalled = WaitForSingleObjectEx(handle, 0, true) != WAIT_TIMEOUT;

…but for auto-reset events that has the side-effect of resetting them. I guess I could try this, but I have a feeling that there should be a less dangerous way…?

bool isSignalled(HANDLE handle)
{
  bool signalled = WaitForSingleObjectEx(handle, 0, true) != WAIT_TIMEOUT;

  // warning - event is now reset. Maybe need to wrap this in a critical section or similar?

  if (signalled)  
    SetEvent(handle);

  return signalled; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:01:25+00:00Added an answer on May 22, 2026 at 3:01 am

    I don’t see a simple way to do it. You could “mock” the event for testing purposes.

    Wrap the event in an C++ object, and change all the code to use its methods.

    class MockEvent {
      public:
        MockEvent () : m_handle(::CreateEvent(NULL, TRUE, FALSE, NULL) {}
        ~MockEvent () { ::CloseHandle(m_handle); }
    
        BOOL Set() { return ::SetEvent(m_handle); }
    
        DWORD Wait(DWORD timeout = INFINITE) {
          return ::WaitForSingleObject(m_handle, timeout);
        }
    
      private:
        HANDLE m_handle;
        // Do not implement copy or assignment:
        MockEvent(const MockEvent &);
        MockEvent &operator=(const MockEvent &);
    };
    

    Then you’ll want to use some sort of reference counted pointer that can be passed around and copied the way the original HANDLE can be:

    typedef std::tr1::shared_ptr<MockEvent> MockEventPtr;
    

    Replace all your code that uses the raw HANDLE with a MockEventPtr.

    // Original code:
    HANDLE hEvent = CreateEvent(NULL, true, false, NULL);
    // Becomes:
    MockEventPtr pEvent(new MockEvent);
    
    // Original code:
    SetEvent(hEvent);
    // Becomes:
    pEvent->Set();
    

    And so on.

    Now, for your diagnostic harness, you can extend MockEvent to keep track of the state and expose a method to show the current state.

    class MockEvent {
      public:
        MockEvent () :
            m_handle(::CreateEvent(NULL, TRUE, FALSE, NULL),
            m_signaled(false) {
          ::InitializeCriticalSection(&m_cs);
        }
        ~MockEvent () {
          ::DeleteCriticalSection(&m_cs);
          ::CloseHandle(m_handle);
        }
    
        BOOL Set() {
          ::EnterCriticalSection(&m_cs);
          m_signaled = true;
          BOOL result = ::SetEvent(m_handle);
          ::LeaveCriticalSection(&m_cs);
          return result;
        }
    
        DWORD Wait(DWORD timeout = INFINITE) {
          ::EnterCriticalSection(&m_cs);
          DWORD result = ::WaitForSingleObject(m_handle, timeout);
          if (result == WAIT_OBJECT_0) {
            m_signaled = false;
          }
          ::LeaveCriticalSection(&m_cs);
          return result;
        }
    
        // The result of this may be obsolete by the time you get it.
        bool IsSignaled() const { return m_signaled; }
    
      private:
        HANDLE m_handle;
        bool m_signaled;
        CRITICAL_SECTION m_cs;
        // Do not implement copy or assignment:
        MockEvent(const MockEvent &);
        MockEvent &operator=(const MockEvent &);
    };
    

    This is untested code. In real code, I’d wrap the CRITICAL_SECTION, too. Note that the result of IsSignaled may be obsolete the moment you get it, if another thread changes the state. I’m assuming this is for testing code that will check at a time when the state should be a certain way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning to use Auto reset Event Handle for Inter Thread communication. EventWaitHandle
I'm working on databases that have moving tables auto-generated by some obscure tools. By
I have a method that contains an asynchronous call like this: public void MyMethod()
I have read the documentation on this and I think I understand. An AutoResetEvent
We have an auto-complete list that's populated when an you send an email to
For example if I have an auto-numbered field, I add new records without specifying
I have the simple class using auto-implemented properies: Public Class foo { public foo()
I have written a CSS sprite auto-generator which takes selected images out of the
Does PHP have a method of having auto-generated class variables? I think I've seen
What I have: LINQ to SQL auto generated class, which corresponds to the shape

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.