G
Guest
We have an application talking to a phone via a USB-cable, using
System.IO.Ports.SerialPort.
If the user unplugs the USB cable from the PC before the application has
called SerialPort.Close(), we (nearly always) get an unhandled exception
window (<application> has encountered an error and needs to close. Please
inform Microsoft about the error. Etc. etc.) about a
System.ObjectDisposedException.
Calling close after the USB cable has been unplugged does not help - it
throws an exception that we can catch (UnauthorizedAccessException), but the
error report comes anyway.
We believe this happens when the CLR disposes the serial stream. It does not
happen on "our" thread - we can't catch it.
When the USB cable is unplugged the COM port will disappear from the system
- it is not returned from SerialPort.GetPortNames().
We have the same behaviour with both SonyEricsson phones (T610, T630) and
Siemens (S55).
The phone does not appear in Safely Remove Hardware - neither when attached
or removed.
The problem does not arise when running in the debugger.
Here is sample code that exposes the problem. Attach a (turned on) cell
phone via a USB cable before running the sample - change the COM-port to
match the one you are using.
====
using System;
using System.IO.Ports;
namespace SerialPortUsbCrash {
class Program {
static void Main(string[] args) {
SerialPort serialPort = null;
serialPort = new SerialPort("COM12", 19200, Parity.None, 8,
StopBits.One);
serialPort.Open();
Console.Write("Pull out the USB cable, and push return");
Console.ReadLine();
try {
serialPort.Close();
} catch (Exception ex) {
Console.WriteLine("Got exception closing SerialPort: " + ex);
}
Console.WriteLine("- THE END -");
Console.ReadLine();
}
}
}
====
Any advice? Fix? Patch?
Best regards,
Speakanet
System.IO.Ports.SerialPort.
If the user unplugs the USB cable from the PC before the application has
called SerialPort.Close(), we (nearly always) get an unhandled exception
window (<application> has encountered an error and needs to close. Please
inform Microsoft about the error. Etc. etc.) about a
System.ObjectDisposedException.
Calling close after the USB cable has been unplugged does not help - it
throws an exception that we can catch (UnauthorizedAccessException), but the
error report comes anyway.
We believe this happens when the CLR disposes the serial stream. It does not
happen on "our" thread - we can't catch it.
When the USB cable is unplugged the COM port will disappear from the system
- it is not returned from SerialPort.GetPortNames().
We have the same behaviour with both SonyEricsson phones (T610, T630) and
Siemens (S55).
The phone does not appear in Safely Remove Hardware - neither when attached
or removed.
The problem does not arise when running in the debugger.
Here is sample code that exposes the problem. Attach a (turned on) cell
phone via a USB cable before running the sample - change the COM-port to
match the one you are using.
====
using System;
using System.IO.Ports;
namespace SerialPortUsbCrash {
class Program {
static void Main(string[] args) {
SerialPort serialPort = null;
serialPort = new SerialPort("COM12", 19200, Parity.None, 8,
StopBits.One);
serialPort.Open();
Console.Write("Pull out the USB cable, and push return");
Console.ReadLine();
try {
serialPort.Close();
} catch (Exception ex) {
Console.WriteLine("Got exception closing SerialPort: " + ex);
}
Console.WriteLine("- THE END -");
Console.ReadLine();
}
}
}
====
Any advice? Fix? Patch?
Best regards,
Speakanet