3

I got a somehow complex .net console application, that uses WPF for some notification windows and also does some http calls. In very rare cases, that application crashes and the only error message I'm able to get comes from windows event viewer:

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()

I have no idea where the stack trace comes from. Any ideas where to look for this issue? Here are some of my thoughts:

  • Since there is this timer stuff on the stack, could it be related to System.Threading.Timer? Because there are some timers used in code?

  • There is also some http communication involved (httpclient, httpwebrequest) which is using timeouts. Could there be a connection to this error?

Sorry for the very unspecific question, but I'm totally stuck here and just need some kind of starting point.

Steve
  • 213,761
  • 22
  • 232
  • 286
DanielG
  • 1,217
  • 1
  • 16
  • 37
  • 1
    See this, http://stackoverflow.com/questions/4962172/why-does-a-system-timers-timer-survive-gc-but-not-system-threading-timer – H H May 23 '16 at 08:45
  • that's interesting, but I'm not sure if it is related to my issue. And I just realized that the used timer is System.Timers.Timer. Will have a deeper look into it. Thanks! – DanielG May 23 '16 at 08:57
  • The error mentions System.Threading.TimerQueueTimer but that may be underpinning the Systems.Timer too, I don't know. – H H May 23 '16 at 14:39

1 Answers1

1

I had the same problem and find out that the following code cause the exception:

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

In case when the timer is not disposed within a second the exception may occurred. The solution is - to dispose the resetEvent only if the WaitOne method returns true.

begemot
  • 11
  • 1