ObjectDisposedExecption关闭.NET的SerialPort后ObjectDisposedExecption、NET、SerialPort

2023-09-04 01:12:32 作者:无力抗拒想你的夜里.ζ

我使用的是.NET 4的SerialPort对象交谈,连接到COM1的设备。

I am using a .NET 4 SerialPort object to talk to a device attached to COM1.

当我与设备做,我呼吁关闭上的SerialPort。我不调用Dispose,但我认为,关闭和处置的代名词这里。

When I am done with the device, I call Close on the SerialPort. I do not call Dispose, but I believe that Close and Dispose are synonymous here.

通常这个工作得很好。

然而,有时候,我得到下面的异常一段时间后(我见过的范围从5毫秒至175毫秒的时间):

Sometimes, however, I get the following exception some time later (The times I've seen range from 5 ms to 175 ms):

System.ObjectDisposedException: Safe handle has been closed
     at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
     at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
     at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(SafeFileHandle hFile, NativeOverlapped* lpOverlapped, Int32& lpNumberOfBytesTransferred, Boolean bWait)
     at System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()

没有我的code是在栈中。

None of my code is on this stack.

我发现http://blog.zachsaw.com/2010/07/serialport-ioexception-workaround-in-c.html,但解决的办法有没有工作。在进一步的检查,这个问题有一个 IOException异常,而不是的ObjectDisposedException

I found http://blog.zachsaw.com/2010/07/serialport-ioexception-workaround-in-c.html, but the solution there did not work. On further inspection, the issue there is an IOException, not an ObjectDisposedException.

有过多有关问题的帖子时,观察到一个USB转串口设备被拔掉,但COM1是车载,所以它不会意外地消失。

There are a plethora of posts concerning issues observed when a USB-to-serial device is unplugged, but COM1 is onboard, so it isn't vanishing unexpectedly.

问题here也不是我的问题;所述的SerialPort保持活着供其使用的持续时间,并且仅被关闭时,我画完交谈设备。 (一旦我完成,该装置处于一种状态,其中,将不发送任何进一步的数据。)

The problem here is also not my issue; the SerialPort is kept alive for the duration of its use, and is closed only when I am done talking to the device. (Once I am done, the device is in a state where it will not transmit any further data.)

SLaks suggests在入口断点设置为 SafeHandle.Dispose ,以确定当我处理一些我不应该的,但我的攻击,断点数十次。三次都被我一个电话叫 SerialPort.Close ,当我使用的串行设备正在做的,大约一半,其余都在GC线程。其余的似乎是与WPF UI元素。

SLaks suggests setting a breakpoint on the entrance to SafeHandle.Dispose, to determine when I'm disposing something I shouldn't be, but I strike that breakpoint dozens of times. Three times are called by my single call to SerialPort.Close, when I am done using the serial device, and about half the rest are in the GC thread. The remainder seem to be related to WPF UI elements.

我现在很茫然。我在哪里何去何从?

I am now at a loss. Where do I go from here?

有没有办法来确定哪些的SafeHandle属于哪个对象,所以我可以肯定我不会意外地处理它? 有一些咒语以外关闭我需要正确地关闭的SerialPort?

Is there a way to determine which SafeHandle belongs to which object, so I can be certain I'm not disposing it unexpectedly? Is there some incantation other than Close I need to properly shut down a SerialPort?

推荐答案

我有这个问题太,因为我又开始用我从来没有见过它了以下两条规则。

I've had this issue too, and since I started using the following two rules I've never seen it again.

始终调用Close(),随后的Dispose()。 从不重用的SerialPort对象,总是创建一个新的端口需要被重新打开时。

我知道,他们不是什么新闻,但它一直为我工作。

I know, they aren't much news, but its been working for me.