// actually bytes in the buffer to read. // // If you are reading more than one byte at a time from he buffer
// (which this program does not do) you will have the si uation occ ur // where the first byte to arrive will cause the WaitFor ultipleObj ects() // function to stop waiting. The WaitForMultipleObjects ) function
// resets the event handle in m_OverlappedStruct.hEvent o the non- signelead state // as it returns. // // If in the time between the reset of this event and th call to // ReadFile() more bytes arrive, the m_OverlappedStruct. Event hand le will be set again // to the signeled state. When the call to ReadFile() oc urs, it wi ll // read all of the bytes from the buffer, and the progra will // loop back around to WaitCommEvent(). // // At this point you will be in the situation where m_Ov rlappedStr uct.hEvent is set, // but there are no bytes available to read. If you pro eed and ca ll // ReadFile(), it will return immediatly due to the asyn port setu p, but // GetOverlappedResults() will not return until the next character arrives. // // It is not desirable for the GetOverlappedResults() fu ction to b e in // this state. The thread shutdown event (event 0) and he WriteFi le() // event (Event2) will not work if the thread is blocked by GetOver lappedResults(). // // The solution to this is to check the buffer with a ca l to Clear CommError(). // This call will reset the event handle, and if there a e no bytes to read // we can loop back through WaitCommEvent() again, then roceed. // If there are really bytes to read, do nothing and pro eed.