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.exeFramework Version: v4.0.30319Description: The process was terminated due to an unhandled exception.Exception Info: System.ObjectDisposedExceptionStack: 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.推荐答案Microsoft 似乎不会很快修复此错误,并且没有好的解决方法.我花了一年多的时间来断断续续地解决这个问题.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.我的解决方案是使用第 3 方组件.在测试 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.免费快递版本在这里:http://www.componentsource.com/products/commstudio/downloads.html?rv=42917 这篇关于.NET 4 串行端口 ObjectDisposedException 仅适用于 Windows 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-24 14:58