【问题标题】:ObjectDisposedException - DangerousAddRefObjectDisposedException - DangerousAddRef
【发布时间】:2016-05-23 08:40:37
【问题描述】:

我有一个有点复杂的 .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)。可能与此错误有关吗?

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

【问题讨论】:

  • 这很有趣,但我不确定它是否与我的问题有关。我刚刚意识到使用的计时器是 System.Timers.Timer。将对其进行更深入的研究。谢谢!
  • 错误提到 System.Threading.TimerQueueTimer 但这可能也是 Systems.Timer 的基础,我不知道。

标签: c# objectdisposedexception safehandle


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2012-06-03
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多