I have a device (USB->serial) and an application software (.NET 3.5) connected to the device COM port. There is a function which is able to reboot the device (done by sending a serial command to the device).
The problem is, ObjectDisposedException
is thrown by System.dll
when the device is rebooted. (randomly thrown & uncatchable even though I have wrapped the code inside try-and-catch block).
I have found some information online that this exception is due to SerialPort's internal Stream object getting disposed by the garbage collector when the physical port disappears.
I have implemented the coding
i.e. GC.SuppressFinalize(SerialPort)
and
GC.SuppressFinalize(SerialPort.BaseStream)
in connect function,
and also GC.ReRegisterForFinalize
in disconnect function
as suggested here
But it does not help to solve the ObjectDisposedException when USB is unplugged and removed from device manager.
Any suggestion to solve this issue?