I have the same situation as described in the question from the link 2 above, where if I disconnect a device while it is still in use (port open) it will still be present in the array of SerialPort.GetPortNames
until I close the application from which the port has been opened.
How do I know this?
I have been watching the changes in Registry Editor (HKLM\Hardware\DeviceMap\SerialComm
) while disconnecting the device while it's in use and I have noticed that when I close/terminate/kill the application the erroneous port will be removed immediately after that. This leads me to the conclusion that the problem is not in the operating system but in my application.
What did I do to solve the issue?
I have tried to close the port, to dispose the port, to set the variable of the port to null
and then call the garbage collector. None of these seemed to be working.
If my assumption that the inconsistency is caused by my application is correct, then I would like to know whether it is possible to (forcibly) reset the inner state of the application without closing it.
PS: I have been playing with connecting/disconnecting and what I have noticed is that I can make it to show one single port (e.g. COM5) multiple times (as multiple rows) in the Registry Editor. When I terminate the application all of them will disappear.