What is throwing ObjectDisposedException from SerialPort when debugging .NET winform?

Vista SP1 Visual Studio 2008 SP1 .NET 3.5 SP1 C #

I have a winforms application that I am playing with and is using a SerialPort object as a private variable. When the application is compiled and executed, it works great. It also works in debug mode with any breakpoints. 90% of the time, when I stop at a breakpoint and try to execute the code, I get an unhandled exception dialog with these details:

Fixing System.ObjectDisposedException was unhandled Message = "Safe handle closed" Source = "mscorlib" ObjectName = "" Stack trace: at Microsoft.Win32.Win32Native.SetEvent (handle SafeWaitHandle) at System.Threading.EventWaitHandle.Set () at System. IO.Ports.SerialStream.AsyncFSCallback (UInt32 errorCode, UInt32 numBytes, NativeOverlapped * pOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback (UInt32 errorCode, UInt32 numBytes) * NativeOverOVception

Depressing thing: I don't have to iterate over the serial code! I just had to do something with the port. So I could read a string, manipulate a string, add two numbers together, whatever, and then BANG.

Again, this works great when NOT debugging, or when debugging any breakpoints. Something seems to be happening with the breakpoint stop, which causes the CLR to use SerialStream on a different thread.

There is a lot of talk on the internet about issues with updating USB devices causing this. But I am using the motherboard's built-in port on COM1.

I don't think I had this problem in .NET 2.0, so I might have to come back to this ...

I need to simplify my application a bit before I can post code, but has anyone seen this behavior in the debugger before?

Many thanks!

0


source to share


5 answers


I just had the same problem this morning. Surprisingly, it just went away when I disabled the following options in VS2008 Tools-> Options-> Debugging-> General:

  • "Enable Remediation Assistant"
  • "Enable .NET Framework Source Stepping"
  • "Step over properties and operations"
  • "Include property evaluation and other implicit function calls"


I have no idea why, but it worked for me.

+1


source


It is possible that your port is being closed by the operating system as it is not receiving a response from your application (it was stopped at a breakpoint).



+1


source


Well, I'm not sure if this is the answer, but there was something definite about this project. It was originally written in version 2.0 and converted to 3.5 by VS2008. I created a new project in C # -Express 2008 adding the original classes one by one and now it works like a charm! I don't know what is different.

0


source


I have this too. It must be some kind of bug with the debugger. The above tip worked: Disable "Include property evaluations and other implicit function calls".

I have a class with properties that do serial I / O. I thought maybe the debugger was helpful when trying to display the value of a property when I hovered over it, thereby doing the IO from the debugger thread. But it doesn't seem to be the case. I'm really not sure what the reason is.

0


source


I had the same problem and did the following:

serialPortLock = Monitor.TryEnter(serialPort, 3000);
Thread.Sleep(5);
serialPort.Write(msg, 0, msg.Length);

      

and the same for my Read (). Sounds like a good way to get around me!

0


source







All Articles