Safe handle closed (?)  
Author Message
RichardCasey





PostPosted: Visual C# General, Safe handle closed (?) Top


Hello,

We use Visual Studio 2005 and C# to develop an application that interfaces with biomedical device firmware. The firmware sends serial communication messages to the C# application, which then parses the message streams. We routinely get the following error message:

Unhandled Exception:
System.ObjectDisposedException: Safe handle has been closed
at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(SafeFileHandle hFi
le, NativeOverlapped* lpOverlapped, Int32& lpNumberOfBytesTransferred, Boolean b
Wait)
at System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

We're having trouble finding out where in the code this exception is thrown, and it's not clear why it's generated. Does anyone have ideas on how to isolate where in code the error is generated Or what might cause this error



Visual C#16  
 
 
Peter Ritchie





PostPosted: Visual C# General, Safe handle closed (?) Top

It doesn't specifically mention the issue you've detailed, but http://blogs.msdn.com/bclteam/archive/2006/10/10/Top-5-SerialPort-Tips-_5B00_Kim-Hamilton_5D00_.aspx describes some practices when using the SerialPort object that could possibly fix the issue you've described.

Without some sample source though, we won't be able to offer specific guidance. Off hand, do you close you SeriaPort object before the thread is stopped

 
 
Markku Behm





PostPosted: Visual C# General, Safe handle closed (?) Top

Hi

I think you should find all places where you close that serial port.

I suppose that somewhere you call close method and after that you try to read that port.

Or mayby it isn't open at all

Hope this help you find the error.

Markku


 
 
RichardCasey





PostPosted: Visual C# General, Safe handle closed (?) Top

Thanks for the rapid response to my inquiry. I think your suggestions will help us track down the problem with the serial port. Checking into this right now.

 
 
Makuk





PostPosted: Visual C# General, Safe handle closed (?) Top

Hi Gurus,

I am having the exact same problem.

Basically I am using a USB to Serial device and when the USB to Serial Device is unplugged I get the above mentioned exception.

I've followed the example on http://blogs.msdn.com/bclteam/archive/2006/10/10/Top-5-SerialPort-Tips-_5B00_Kim-Hamilton_5D00_.aspx and added the following line to your application's config file to get the earlier behavior and be able to catch the exception.

< xml version ="1.0" >
<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="1"/>
  </runtime>
</configuration>
On my computer where VS2005 is installed, I don't see exception (i've handled it) so when I replug in my USB-Serial device it continues to work.
This is what I want to happen. The big BUT is once I run my application on another computer the exception still occurs. I can't figure out what
is the difference except my computer has VS2005 installed. I've tried to install .NET 2.0 SDK on the other computer but no avail.
Please help! I've run out of ideas and it's a major problem. 
Andrew

 
 
Peter Ritchie





PostPosted: Visual C# General, Safe handle closed (?) Top

Hi Gurus,

I am having the exact same problem.

Basically I am using a USB to Serial device and when the USB to Serial Device is unplugged I get the above mentioned exception.

I've followed the example on http://blogs.msdn.com/bclteam/archive/2006/10/10/Top-5-SerialPort-Tips-_5B00_Kim-Hamilton_5D00_.aspx and added the following line to your application's config file to get the earlier behavior and be able to catch the exception.

< xml version ="1.0" >
<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="1"/>
  </runtime>
</configuration>
On my computer where VS2005 is installed, I don't see exception (i've handled it) so when I replug in my USB-Serial device it continues to work.
This is what I want to happen. The big BUT is once I run my application on another computer the exception still occurs. I can't figure out what
is the difference except my computer has VS2005 installed. I've tried to install .NET 2.0 SDK on the other computer but no avail.
Please help! I've run out of ideas and it's a major problem. 
Andrew
Out-of-the-box USB2COM drivers are notoriously poorly written, especially for asynchronous (overlapped IO). Make sure you have the latest drivers. When working on a Win32 serial-port based application I could not get anything to work until the latest drivers were installed...

 
 
Makuk





PostPosted: Visual C# General, Safe handle closed (?) Top

Hi,

I understand out of the box USB2COM drivers are horrible. But I'm really puzzled what's the difference of from my computer and the other deployment computers I'm using. The driver is the same across all machines.

Andrew