I have an app that manages multiple serialports. It should show what ports are currently connected. For detecting the current ports I use SerialPort.GetPortNames();
. This works fine, if the port is closed before it is removed. However, if the port is disconnected before being closed, SerialPort.GetPortnames();
still returns it, even if it is closed and disposed after. Is there any way to remove the port after it has been physically disconnected?
Code:
try
{
// Read from the port.
}
catch(OperationCanceledException e) // This exception is thrown when the port is disconnected.
{
// Stop other tasks
_port.Close();
_port.Dispose();
}
PS: I am aware of the similarities to the question SerialPort.GetPortNames() behavior but the solution does not seem to work for me.