My application is handling communication via serial ports. The problem of mine is that it has some chance that the the communication cable is unplugged from the computer.
I use USB to Serial cables, which means if they unplug from the computer the COM port disappear. I could restart the service (the read application is a windows service) every time when I lose a com port, but detecting newly connected comports is not as fast as it could be with:
SerialPort.GetPortNames()
I thought I will just let the application to continue the read; as as soon as some one plugs back the cable the comport will be visible again for the service. When I do that I keep getting an error like this:
first chance exception of type 'system.invalidoperationexception' occurred in system.dll
I have two different locations where I catch exception when I read without the comport existence:
//sp is the disconnected serial port
sp.DiscardOutBuffer();
sp.Open();
Even with catching the exceptions my application still crashes, and I have no source to determine where the error is.
I guess I will go with stopping and restarting my service, I just thought I will ask your opinion guys as you might have a better solution.
Edit: I would not say it is a duplicate for the marked question, as it still does not tell me why I am getting the exceptions. Or am I wrong?