gpt4 book ai didi

c# - ObjectDisposedException - DangerousAddRef

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:01 33 4
gpt4 key购买 nike

我得到了一个有点复杂的 .net 控制台应用程序,它使用 WPF 作为一些通知窗口,也进行一些 http 调用。在极少数情况下,该应用程序会崩溃,而我能够获得的唯一错误消息来自 Windows 事件查看器:

Application: xyz.exe
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
Stack:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean ByRef)
at Microsoft.Win32.Win32Native.SetEvent(Microsoft.Win32.SafeHandles.SafeWaitHandle)
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireNextTimers()

我不知道堆栈跟踪来自哪里。任何想法在哪里寻找这个问题?以下是我的一些想法:

  • 既然堆栈上有这个计时器的东西,它会不会与 System.Threading.Timer 有关?因为代码中用到了一些定时器?

  • 还有一些使用超时的 HTTP 通信(httpclient、httpwebrequest)。是否与此错误有关?

很抱歉提出非常不具体的问题,但我完全被困在这里,只需要某种起点。

最佳答案

我遇到了同样的问题,发现是以下代码导致了异常:

using (ManualResetEvent resetEvent = new ManualResetEvent(false))
{
timer.Dispose(resetEvent);
resetEvent.WaitOne(1000);
}

如果计时器没有在一秒内被处理掉,则可能会发生异常。解决方案是 - 仅当 WaitOne 方法返回 true 时才处理 resetEvent。

关于c# - ObjectDisposedException - DangerousAddRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37386373/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com