I'm using mikeobriens HIDLibrary for communicating with a sensor we make.
It's all working, and I have it so it re-enumerates if the device is unplugged then reconnected.
The problem I have is that each time it's reconnected, a new HidLibrary.HidDeviceEventMonitor.DeviceEventMonitor Worker Thread is created, and if the system is run like this for a long period, many threads are created. How can I kill these threads when the device is disconnected?
I believe there is a kind of bug if the HidLibrary.
If you look closer at the HidDevice class you may found that HidDevice.CloseDevice doesn't actually stop event monitoring.
There are two ways to fix it. Either call Dispose instead of CloseDevice in your application:
//_device.CloseDevice(); - dispose will close device automatically
_device.Dispose();
Or set the flag MonitorDeviceEvents to false after CloseDevice call:
_device.CloseDevice();
_device.MonitorDeviceEvents = false;
I would prefer the first one.
N.B. Also be sure that you don't keep a reference to the disconnected device.