ObjectDisposedException if USBtoSerial Port is unplugged

G

Guest

Hello,

I'm writing an Application for a serial device, which is using a
USB-to-Serial Converter. I'm polling that device for new data every 500 ms.
So this is working fine.

But if the customer unpluggs the USB-to-Serial Converter, I get an
System.UnauthorizedAccessException on the next write operation and sometimes
an ObjectDisposedException which is not chatchable after that.

I tried many things and I can reproduce it now with some very simple lines
of code.

First I have to explain, that my System - after playing a lot with plugging
and unplugging USB-to-Serial devices - sometimes takes a few seconds until it
recognizes the device to be unplugged. So I have a few seconds between
unplugging the device and hearing the unplug sound of the system.

Now my simple example:

In a Windows Form, I create a SerialPort and open that port.
Now I unplugg the USB-to-Serial converter.
Then I close the SerialPort (before the unplugg sound is played)
Then, after a few seconds, exactly when the unplugged sound is played, I get
that "ObjectDisposedException, safe handle has been closed".

I also tried to create a new port instead of closing it. In this case I get
the UnauthorizedAccessException when the unplugged sound is played - or when
the form is closed.

As I figured out, if any command of the SerialPort is called between
unplugging the device and the unplugg sound of the system, a not catchable
exception will be thrown.

How can I avoid that Exception?
Or is it possible to catch (and ignor) it?

Thanks in advance.
Robert
 
L

Luke Zhang [MSFT]

Hello Robert,

I think it is impossible to avoid of the problem, since it is a hardware
issue, and .NET application totally reply on windows system to recognize
and connect to the Serial port.

We may find some way to catch the exceptions, and tell the user to check
their device. For example, in Application.ThreadException event. In the
application, we cannot expect when user will unplug it or put it back, so
it is hard to close/ reopen the port to avoid the application. We may catch
the exception Application.ThreadException and tell customer there may be
an error with the USB-to-Serial converter. And, for such a hardware change,
we had better recommend user quit current application and enter again.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hello Luke,

unfortunately, the Application.ThreadException will not catch this exception.
Well, the AppDomain.CurrentDomain.UnhandledException will do, but after
that, I cannot avoid that the application is terminated immediately.

Do you see any chance to avoid the termination of the application?
(The place where this exception occures is absolutley uncritical for the
application).

Sincerely,
Robert
 
L

Luke Zhang [MSFT]

Hello Robert,

Thank you for the update. AppDomain.CurrentDomain.UnhandledException can
catch an exception, but the exception will be still raised after that. So
that the application is terminated immediately.

Sincerely,

Luke Zhang

Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

I was looking at the ability to switch from using interop (for serial comm)
in our application to using the SerialPort class. However, the appearent
inability for the class to deal with removeable comm ports (and shutting down
the app) will definitely prevent it. Considering how many PC's no longer
have built in serial ports, the inability to deal with the port going away
really makes this class unusable for any non trivial use.

When using InterOp to access the Win32 functions WaitCommEvent() and
SetCommMask(), unplugging the USB port does not result in an exception that
causes an application shutdown.

The receivers WaitCommEvent call simply returns without showing any data has
been received.
Then calling SetCommMask to reset the event mask returns a failure, which
allows the application to know the comm port is no longer available.

MS really needs to look at fixing this. With the effort that has been put
into the creation of the SerialPort class, leaving it in this state would be
a real waste.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top