我有一个C#应用程序,它使用消息命中ObjectDisposedException
安全手柄已关闭
一旦我启动应用程序,就会发生这种情况.
可悲的是,堆栈跟踪真的没有用(见下文).有什么方法可以让我确定在这里异步尝试的呼叫是什么?
DoAsyncCall()是否真的意味着异步方法调用?
mscorlib.dll中!System.Threading.EventWaitHandle.Set()+ 0xe字节
mscorlib.dll中!System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(System.Runtime.Remoting.Messaging.IMessage MSG)+ 0x12f字节
mscorlib.dll中! System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(System.Runtime.Remoting.Messaging.IMessage味精,System.Runtime.Remoting.Messaging.IMessageSink replySink = {System.Runtime.Remoting.Messaging.AsyncResult})+ 0x279个字节
的mscorlib .DLL!System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.DoAsyncCall()+ 0x32字节mscorlib.dll中!System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(对象o)+ 0×28字节
的mscorlib.dll!System.Threading._ThreadPoolWaitCallback .WaitCallback_Context(对象状态)+值为0x2F字节
mscorlib.dll中!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext的ExecutionContext,System.Threading.ContextCallback回调,对象状态)+ 0x6f字节
mscorlib.dll中!System.Threading._ThreadPoolWaitCallback .PerformWaitC allbackInternal(System.Threading._ThreadPoolWaitCallback tpWaitCallBack)+ 0x53字节
mscorlib.dll中!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(对象状态)+ 0×59字节
这是我曾经一直使用.NET 2.0中的串行端口类的问题.有人建议升级到.NET 4可以解决问题......几乎在所有情况下都能解决.
如果我使用.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()
Run Code Online (Sandbox Code Playgroud)
再次,大多数时候它工作正常.事实上,我甚至无法在自己的计算机上重现这个问题.这发生在我的一个朋友身上.是否有任何方法可以捕获此错误?
编辑:我能够自己重现这种行为.我现在正在进行实验,但仍然难以理解导致.NET 4.0中出现此错误的原因.
编辑2:这似乎只发生在Windows 7上.在XP上拔掉并插入USB转串口适配器可以很好地工作.