这是一个问题,我曾经一直在使用.NET 2.0中的串行端口类。有人建议升级到.NET 4可以解决这个问题......并且几乎在所有情况下都这样做。.NET 4串行端口ObjectDisposedException仅在Windows 7上
如果我使用.NET内置的串口类与USB串口适配器进行通信,并且在端口打开时适配器意外拔出,有时候会出现未处理的异常:
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()
再次,大多数时候它工作正常。事实上,我甚至无法在我自己的电脑上重现这个问题。这发生在我的一个朋友身上。有什么办法来捕捉这个错误?
编辑:我能够重现自己的行为。我现在正在做实验,但仍然难以理解.NET 4.0中导致这种错误的原因。
编辑2:这似乎只发生在Windows 7上拔下,并在USB转串口适配器插入XP精美的作品。
奇怪的是,我永远无法工作。我早已放弃了微软内置的串行类,并已与第三方一起。 – Brad 2011-01-31 15:22:56
够公平的,我不认为有人会试图说这是一个糟糕的选择:) – 2011-01-31 15:24:49