private static void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) { WatchdogTimeout callback = OnWatchdogTimeout; if (callback != null) { ThreadWatchdogInfo timedOut = null; lock (m_threads) { int now = Environment.TickCount & Int32.MaxValue; foreach (ThreadWatchdogInfo threadInfo in m_threads.Values) { if (threadInfo.Thread.ThreadState == ThreadState.Stopped || now - threadInfo.LastTick >= WATCHDOG_TIMEOUT_MS) { timedOut = threadInfo; m_threads.Remove(threadInfo.Thread.ManagedThreadId); break; } } } if (timedOut != null) { callback(timedOut.Thread, timedOut.LastTick); } } m_watchdogTimer.Start(); }
/// <summary> /// Check watched threads. Fire alarm if appropriate. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) { WatchdogTimeout callback = OnWatchdogTimeout; if (callback != null) { List <ThreadWatchdogInfo> callbackInfos = null; lock (m_threads) { int now = Environment.TickCount & Int32.MaxValue; foreach (ThreadWatchdogInfo threadInfo in m_threads.Values) { if (threadInfo.Thread.ThreadState == ThreadState.Stopped) { RemoveThread(threadInfo.Thread.ManagedThreadId); if (callbackInfos == null) { callbackInfos = new List <ThreadWatchdogInfo>(); } callbackInfos.Add(threadInfo); } else if (!threadInfo.IsTimedOut && now - threadInfo.LastTick >= threadInfo.Timeout) { threadInfo.IsTimedOut = true; if (threadInfo.AlarmIfTimeout) { if (callbackInfos == null) { callbackInfos = new List <ThreadWatchdogInfo>(); } callbackInfos.Add(threadInfo); } } } } if (callbackInfos != null) { foreach (ThreadWatchdogInfo callbackInfo in callbackInfos) { callback(callbackInfo.Thread, callbackInfo.LastTick); } } } m_watchdogTimer.Start(); }