title: When using WriteFile in C# for communication with HID device my program halts
body: <p>At the end of a data transfer session with my HID device my software has a ~20% chance to halt on the function WriteFile. </p>
  
  <p>This is written in C#, and I can't figure out why this is happening. A 20% error rate per packet is simply unacceptable and I am having a hard time getting to the bottom of it, I was wondering if it might be something with my declarations or data types?</p>
  
  <p>This is the import</p>
  
  <pre><code>[DllImport("kernel32.dll")] 
      static public extern int WriteFile(int hFile, ref byte lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, int lpOverlapped);
  </code></pre>
  
  <p>And this is the call to the function that is halting</p>
  
  <pre><code>Result = USBSharp.WriteFile(hidHandle, ref outputReportBuffer[0], outputReportBuffer.Length, ref NumberOfBytesWritten, 0);
  </code></pre>
  
  <p>The handle is confirmed to be valid and the rest is fairly self explanatory...</p>
  
  <p>The function simply never returns.  I've looked this issue up online a few different locations and mostly nobody's fixes apply to me. I would just thread it and re-call it if it fails but doing that 20% of the time on hundreds of packets is simply... awful.</p>
  
  <p>I am using windows 7, C#, .NET 4.0, and the HID device is not halting it is still active and running - not only that, but the entire data transfer happens properly and this call happens at the very end to complete the transaction and then halts (even though I already have all the data).  Unfortunately I can't just ignore this final part of the transaction because this data needs to be 100% maintained or else bad, bad, BAD things will happen to the users.</p>