我继承的代码不断崩溃,但出现以下错误(根本未更改):

System.ObjectDisposedException: Safe handle has been closed
   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.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.
          ExecuteCodeWithGuaranteedCleanup(
          TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(
          ExecutionContext executionContext, ContextCallback callback,
          Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
          ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

这只是在以前的开发人员添加了AppDomain.UnhandledException Event时才发现的。

如果删除它,该应用程序只会崩溃并显示Dr Watson消息(发送反馈等),而不是通常的.NET对话框(带有Continue选项和堆栈跟踪)。

我已经检查过了,它与Thread.Abort没有关系

我该如何尝试从堆栈跟踪中查找出现此问题的原因,而不是出现在应用程序的代码中?

最佳答案

从引用System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()和Microsoft.Win32.UnsafeNativeMethods的事实来看,我会冒用具有内部线程访问端口的COM组件的COM组件的危险。用于串行或TCP/IP数据。

看起来该线程在其启动序列期间引发了异常。可能是它试图访问不可用或不存在的端口。这将失败,并且异常不会得到处理,因此会在代码中传播回来。

尝试记录来自UnhandledException事件的更多信息,以了解从何处开始。

09-10 04:09
查看更多