Multiple Applications Based on CyUSB.dll Hang On Disconnection of a Device Communicating with One of Them – KBA89229

Version: **


Question: When communicating with two devices using two applications created with CyUSB.dll, the disconnection of a device communicating with one of the applications hangs the other application. What is going wrong?



The cause of the problem is that both of the applications call the event handler corresponding to the disconnect event. The solution is to check in the event handler which device was disconnected.

This problem occurs because both devices are attached to the CyUSB.sys driver. The driver reports a disconnect event when either one of the devices attached to it is disconnected, calling the handler for the disconnect event. So when you disconnect a device communicating with the first application, the second application still executes the event handler for a disconnect event.

You should check in the event handler in the application if the disconnected device is the one that it was communicating with. If you don’t check, the application will execute further commands as if its device is now disconnected, which will halt, or hang, the communication.

Here is an example of the device removed event handler:

void usbDevices_DeviceRemoved(object sender, EventArgs e) {   USBDevice dev = usbDevices[0x04B4, 0x1004]; // if MyDevice is still connected do not stop the communication.   if (dev == null)   {     MyDevice = null;     EndPoint = null;     SetDevice();   } }