诊断ObjectDisposedException"安全句柄已关闭"

mch*_*chr 14 c# debugging

我有一个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字节

mch*_*chr 16

问题是由于我使用了using(){}块引起的.

    using (WaitHandle handle = asyncResponse.AsyncWaitHandle)
    {
      asyncResponse.AsyncWaitHandle.WaitOne();
      string response = asyncRequest.EndInvoke(asyncResponse);
      asyncResponse.AsyncWaitHandle.Close();
      return response;
    } 
Run Code Online (Sandbox Code Playgroud)

当调用线程被中断时,using块仍然在WaitHandle上调用Close.


SLa*_*aks 14

您正在处理仍由其他线程使用的内容.

  • @mchr:Debug,Break At Function,`System.Runtime.InteropServices.SafeHandle.Dispose`. (5认同)