title: WaitForSingleObject() gets signalled without occurrence of awaited event
body: <p>I'm writing a Class for COM ports in C++ using win-api. Righ now I test the functionality on RS232 with connected Rx and Tx pins.</p>
  
  <p>I've encountered somewhat weird problem. I use separate thread for reading from the COM port. Within the thread I use <code>SetCommMask</code>, <code>WaitCommEvent</code> and <code>WaitForSingleObject</code> to wait for char arrival into buffer. However the <code>WaitForSingleObject</code> tends to exit without actually receiving any chars.</p>
  
  <p>I assumed this was caused by wrong use of mentioned functions, but then I discovered, that premature exit does not occur every time (first time always works as intended). </p>
  
  <p>In second go the thread enters the waiting state and exits a while later proceeding to <code>ReadFile</code>, where it waits indefinitely, because the buffer is empty, no data is to be sent and Total timeout is not used.</p>
  
  <p>I've already been advised to simply use <code>ReadFile</code> and process only the data I acquire, however I use another thread to check if the communication channel has been disconnected and right now I need to distinguish between waiting for data and reading data. </p>
  
  <p>Calling <code>ClearCommError</code> to check input buffer with <code>ReadFile</code> is not an option, because in such a case <code>InQue</code> is always 0. Therefore I cannot tell whether <code>ReadFile</code> is actually reading or waiting.</p>
  
  <pre><code>    //following code runs in separate thread
      DWORD dwEventMask1, dwEventMask2, LastError, Status;
      OVERLAPPED Overlapped; HANDLE Serial_Port_Handle;
      std::string stringBuffer("");
      const size_t ReadBufferLength = 256;
      char tempBuffer[ReadBufferLength];
  
      GetCommMask(Serial_Port_Handle, &amp;dwEventMask1);
  
  
  
      if (dwEventMask1)   // Before starting the thread I check the state of Input Buffer with GetCommError().
      {                   // If Buffer is not empty, CommMask is set to 0 signaling there is no need for waiting.
          Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  
          //wait for comm event
          if (!WaitCommEvent(Serial_Port_Handle, &amp;dwEventMask1, &amp;Overlapped))
          {
              if ((LastError = GetLastError()) == ERROR_IO_PENDING)
              {
                  Waiting = true; //signal bool for synchronization purposes
                  if ((Status = WaitForSingleObject(Overlapped.hEvent, INFINITE)) == WAIT_OBJECT_0)
                  {
                      GetCommMask(Serial_Port_Handle, &amp;dwEventMask2);
                      Waiting = false;
                      CloseHandle(Overlapped.hEvent); 
                      // I close handle and set all members of Overlapped struct to 0
                  } 
  
                  if (dwEventMask2 !== dwEventMask1) // check if wait have not exited because of SetCommMast()
                      return;
              }
          }
      }
      do  // this loop reads from input buffer until empty
      {
          Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);//set up read overlapped operation
  
          if (ReadFile(Serial_Port_Handle, tempBuffer, ReadBufferLength - 1, &amp;NoBytesRead, &amp;Overlapped)) //start read opperation
          { //Read operation done on 1 go
              GetOverlappedResult(Serial_Port_Handle, &amp;Overlapped, &amp;NoBytesRead, FALSE); //get NoBytesRead
  
              CloseHandle(Overlapped.hEvent)
              stringBuffer.append(tempBuffer, (size_t) NoBytesRead); // save read data
          }
          else if ((LastError = GetLastError()) == ERROR_IO_PENDING) //operation not yet complete
          {
              GetOverlappedResult(Serial_Port_Handle, &amp;Overlapped, &amp;NoBytesRead, TRUE); // wait for completion
              stringBuffer.append(tempBuffer, (size_t)NoBytesRead);
          }
          else
          {
              CloseHandle(Overlapped.hEvent)
              return;
          }
      } while ((NoBytesRead == (ReadBufferLength - 1)) &amp;&amp; NoBytesRead);
      // Loop runs while tempBuffer's full capacity is used.
      // I realize that since I don't use Total Timeout there is a possibility
      // of the loop getting stuck. If you can suggest other solution than using
      // Total Timeout or use GetCommError() to get input buffer state, please do so.
      return;
  </code></pre>
  
  <p>This code is somewhat simplified (checking for return values, ets.).</p>
  
  <p>1) Have any of you experienced such a behaviour?</p>
  
  <p>2) I use OVERLAPPED operations in the code. After the operation exits I always use <code>CloseHandle</code> and reinitialize the OVERLAPPED structure before using it for another operation. Is this correct or is resetting the structure sufficient?</p>