.NET 4 串行端口 ObjectDisposedException 仅适用于 Windows 7 [英] .NET 4 Serial Port ObjectDisposedException on Windows 7 Only

查看:25
本文介绍了.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.

推荐答案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆