ObjectDisposedException仅在Windows

ObjectDisposedException仅在Windows

本文介绍了.NET 4串行端口ObjectDisposedException仅在Windows 7上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我曾经在.NET 2.0中使用串行端口类的问题。有人建议升级到.NET 4将解决问题,而且几乎在所有情况下都可以。

This is a problem I used to have all the time with the serial port class in .NET 2.0. It was suggested that upgrading to .NET 4 would fix the problem... and it did in almost all cases.

如果我正在使用串行端口类内置到.NET与USB到串行适配器通信,并且适配器在端口打开时意外拔出,有时我收到一个未处理的异常:

If I am using the serial port class built-in to .NET to communicate with a USB-to-serial adapter, and the adapter is unexpectedly unplugged while the port is open, on occasion I get an unhandled exception:

Application: test.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
Stack:
   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean ByRef)
   at System.StubHelpers.StubHelpers.SafeHandleAddRef(System.Runtime.InteropServices.SafeHandle, Boolean ByRef)
   at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(Microsoft.Win32.SafeHandles.SafeFileHandle, System.Threading.NativeOverlapped*, Int32 ByRef, Boolean)
   at System.IO.Ports.SerialStream+EventLoopRunner.WaitForCommEvent()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

再次,大多数时候它工作正常。其实我甚至不能在自己的电脑上重现这个问题。这发生在我的一个朋友身上。有没有什么办法来解决这个错误?

Again, most of the time it works fine. In fact, I can't even reproduce the problem on my own computers. This happens to a friend of mine. Is there any way at all to trap this error?

编辑:我可以自己重现这个行为。我现在正在尝试,但仍然感到遗憾的是在.NET 4.0中导致这个错误。

I was able to reproduce the behavior myself. I am experimenting now, but still am stumped as to what causes this error in .NET 4.0.

编辑2:这似乎是>只有发生在Windows 7上。拔下并插入XP上的USB到串行适配器工作得很好。

Edit 2: This seems to only happen on Windows 7. Unplugging and plugging in the USB-to-serial adapter on XP works beautifully.

推荐答案

p>似乎微软不会很快修复这个错误,而且没有很好的解决方法。我花了一年多的时间来处理这个问题。

It seems that this bug won't be fixed by Microsoft any time soon, and that there is no good workaround. I've spent over a year working on this problem off-and-on.

我的解决方案是使用第三方组件。测试15+以后,我发现唯一一个真正有效的是CommStudio。

The solution for me was to use a 3rd party component. After testing 15+, I've found that the only one that really works is CommStudio.

免费快递版本在这里:

The free express version is here: http://www.componentsource.com/products/commstudio/downloads.html?rv=42917

这篇关于.NET 4串行端口ObjectDisposedException仅在Windows 7上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:23