Search code examples
vb.nethid

ManualResetEvent.WaitOne(1) waits more than 1ms?


I am acquiring data with an HID device. The code below is a rough outline of my timing mechanism.

Dim CANTimer as New System.Diagnostics.Stopwatch
Dim resetEvent as New Threading.ManualResetEvent(False)
....

CANTimer.Start()
ResetEvent.WaitOne(1)
CANTimer.Stop()

Timing this, I'll usually get times of 3ms, which is the delay I'd expect the HID transfer to take... and about every 4th or 5th iteration will take 20ms. These numbers don't really change regardless of what I set my timeout in milliseconds to be.

Why does the ResetEvent not timeout at one ms?

and.. Closer to the metal, why is it that the HID transfers seem to take either 3ms or 20ms (never 15ms, etc.)... What is happening when the transfer takes 20ms?


Solution

  • Timer resolution on Windows is only 15.625 msec by default. You can crank it up to a millisecond by pinvoking timeBeginPeriod(1) but this has a system-wide affect. Do treat time-outs like true timeouts, not a way to count milliseconds.