2010-09-28 69 views
8

这是一个问题,我曾经一直在使用.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精美的作品。

回答

4

加入以下的app.config

<runtime> 
<legacyUnhandledExceptionPolicy enabled="1"/> 
</runtime> 

很适合我。请参阅Problem with SerialPort

+0

奇怪的是,我永远无法工作。我早已放弃了微软内置的串行类,并已与第三方一起。 – Brad 2011-01-31 15:22:56

+0

够公平的,我不认为有人会试图说这是一个糟糕的选择:) – 2011-01-31 15:24:49

1

(我知道这是一个2岁的问题,但它看起来像问题仍然是相关的,所以我张贴一些代码,我发现,我想)

我在.NET 2.0 VB项目中遇到了类似的问题。通常,当断开USB到串行适配器的连接时,我会得到一个使程序崩溃的“UnauthorizedAccessException”。

我还没有机会将该项目移植到.NET 4.x。另外,我了解我看到的异常与OP提到的异常不同,但我认为潜在的问题可能是相同的,所以我想分享我找到的解决方案。

http://go4answers.webhost4life.com/Example/unauthorizedaccessexception-6130.aspx

实现这一页面底部的代码解决了这个问题对我来说。 下面是实际的VB.NET类我用:

Option Explicit On 
Option Strict Off   ' so we can use late binding (1) 

Imports System.IO.Ports 

''' <summary> 
''' This is a drop-in replacement for .net built-in SerialPort class. 
''' It avoids "UnauthorizedAccessException" errors that sometimes happen 
''' when disconnecting and reconnecting USB-to-serial adapters. 
''' </summary> 
''' <remarks>http://go4answers.webhost4life.com/Example/unauthorizedaccessexception-6130.aspx</remarks> 

Public Class ExSerialPort 

    Inherits SerialPort 

    Public Sub New() 
     MyBase.New() 
    End Sub 

    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
     Dim mytype As Type = GetType(SerialPort) 
     Dim field As Reflection.FieldInfo = mytype.GetField("internalSerialStream", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic) 
     Dim stream As Object = field.GetValue(Me) 

     If Not stream Is Nothing Then 
      Try 
       stream.Dispose() ' (1) 
      Catch ex As Exception 
       ' do nothing 
      End Try 
     End If 
     MyBase.Dispose(disposing) 
    End Sub 

End Class 
1

我也有这个问题,但我发现它触发一个断陷事件,当适配器拔掉一个.NET库之后,它正常断开。当适配器插回时,您可以随时重新连接。 库的名称是ZylSerialPort.NET。