Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
my HID device sends IN report every 200 msec. I call THid::Execute every 10 msec. However, I lose 20% of IN reports. Input buffer count is 512.

When I increase timeout value for WaitForSingleObject I lose less reports. But I want the WaitForSingleObject function to work with zero timeout.

C++
void THid::Execute(void)
// Execute device
{
THidReportExt ReportExt;
HANDLE     Event;
OVERLAPPED Overlapped;
DWORD ReadBytes;

   if(!Handle) {
      return;
   }

   Event = CreateEvent(NULL, TRUE, FALSE, NULL);
   Overlapped.hEvent = Event;
   Overlapped.Offset = 0;
   Overlapped.OffsetHigh = 0;

   while(true) {
      if(!ReadFile(Handle, &ReportExt, Capabilities.InputReportByteLength, &ReadBytes, &Overlapped)) {
         if(GetLastError() != ERROR_IO_PENDING) {
            return;
         }
         DWORD Result = WaitForSingleObject(Event, 0);
         if(Result == WAIT_TIMEOUT || Result == WAIT_ABANDONED) {
            CancelIo(Handle);
            return;
         }
         if(Result != WAIT_OBJECT_0) {
            return;
         }
         if(!GetOverlappedResult(Handle, &Overlapped, &ReadBytes, FALSE)) {
            return;
         }
      }
      if(ReadBytes != (DWORD)Capabilities.InputReportByteLength) {
         return;
      }
      // call some callback
   }
} // Execute


This is how I open the file:

C++
Handle = CreateFile(Path,
      GENERIC_WRITE | GENERIC_READ,
      FILE_SHARE_WRITE | FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
      NULL);
Posted
Updated 21-Apr-14 10:29am
v2

1 solution

Using WaitForSingleObject with zero timeout is apparent nonsense. Timeout should be more than zero, considerably more; and infinite value is also possible.

I don't know if I should explain to you how to use event handles (event objects) and their purpos, but you apparently have no clue, at the moment. Let's do this: start with reading the standard MSDN documentation on them, and, if you still not clear about this topic, ask some further questions.

—SA
 
Share this answer
 
Comments
gaminn 22-Apr-14 11:24am    
I really can't see anything bad about my code (apart from not closing event handle in Execute function). I'm creating event, passing it to ReadFile function and then waiting zero miliseconds for the event to be signaled. I don't want my Execute function to block, this is the reason for zero ms wait.

I would appreciate your explanation and help.
Sergey Alexandrovich Kryukov 22-Apr-14 12:32pm    
All wrong. You simply have no clue what the event is for. It's for keeping a thread in a wait state in some other thread. If you wait zero time, event is not needed at all.
—SA
gaminn 22-Apr-14 12:53pm    
I understand events. I know it's redundant to use event in my case. I think, that my problem comes from calling CancelIo().

My question is how to read from HID without blocking my thread - if there is an IN report in buffer, then return it, otherwise return some error.
Sergey Alexandrovich Kryukov 22-Apr-14 13:29pm    
You just have proven the opposite, very clearly. No, you don't. Sorry, nothing to discuss here. Study the subject matter.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900