HIDDevice.Removed Event occurs when not unplugged
simplifirepyroneil opened this issue · 0 comments
I have a custom HID USB device that after running for hours will suddenly return all 0x0 bytes on reads then the HIDDevice.Removed event occurs about a third of a second later. The ManagementEventWatcher does not show any arrival/removal event for the device. Restart the app and all is well again.
The reader loop does check the read result for HidDeviceData.ReadStatus.Success and when it blows up it still has the read status as success.
Couple of questions - What is causing the reads to return a report that has not had data read into it? What is causing the removal event to be generated?
Opening looks like this:
_ClockDevice = DerivedHidDevices<HidFastReadDevice>.Enumerate(VendorId, ProductId).FirstOrDefault();
if (_ClockDevice != null)
{
_ClockDevice.OpenDevice();
_ClockDevice.Inserted += DeviceAttachedHandler;
_ClockDevice.Removed += DeviceRemovedHandler;
_ClockDevice.MonitorDeviceEvents = true;
UseExternalClock = true;
USBConnected = true;
FSKDevicePresent = true;
ReaderTask = new Task(Reader, TaskCreationOptions.LongRunning);
ReaderTask.Start();
}
Reader task:
while (FSKDevicePresent && UseExternalClock)
{
HidReport report = await _ClockDevice?.FastReadReportAsync();
if (report == null || report.ReportId != 0 || report.ReadStatus != HidDeviceData.ReadStatus.Success )
continue;