public static void ExitProcessDelayed(TimeSpan delay, int exitCode = 0) { if (delay == TimeSpan.Zero) { Utils.TerminateSelf(exitCode); return; } TaskFactoryExtensions.StartNewDelayed(Task.Factory, (int)delay.TotalMilliseconds, delegate() { Utils.TerminateSelf(exitCode); }); }
private void UpdateCallback(object state) { if ((this.IsDisposed || !this.IsRunning ? 0 : (Interlocked.CompareExchange(ref this.m_currentThreadId, Thread.CurrentThread.ManagedThreadId, 0) == 0 ? 1 : 0)) == 0) { return; } DateTime now = DateTime.Now; int num1 = (int)(now - this.m_lastUpdateTime).TotalMilliseconds; long num2 = 0L; long num3 = 0L; int num4 = 0; try { Stopwatch stopwatch1 = Stopwatch.StartNew(); IMessage message; while (this.m_messageQueue.TryDequeue(out message)) { try { message.Execute(); } catch (Exception ex) { Area.logger.Error <Area, Exception>("Exception raised when processing Message in {0} : {1}.", this, ex); } } stopwatch1.Stop(); num2 = stopwatch1.ElapsedMilliseconds; this.m_isUpdating = true; foreach (TimedTimerEntry timedTimerEntry in Enumerable.Where <TimedTimerEntry>((IEnumerable <TimedTimerEntry>) this.m_pausedTimers, (Func <TimedTimerEntry, bool>)(timer => timer.Enabled))) { this.m_timers.Push(timedTimerEntry); } Stopwatch stopwatch2 = Stopwatch.StartNew(); TimedTimerEntry timedTimerEntry1; while (((timedTimerEntry1 = this.m_timers.Peek()) == null ? 0 : (timedTimerEntry1.NextTick <= DateTime.Now ? 1 : 0)) != 0) { TimedTimerEntry timedTimerEntry2 = this.m_timers.Pop(); if (!timedTimerEntry2.Enabled) { if (!timedTimerEntry2.IsDisposed) { this.m_pausedTimers.Add(timedTimerEntry2); } } else { try { timedTimerEntry2.Trigger(); if (timedTimerEntry2.Enabled) { this.m_timers.Push(timedTimerEntry2); } ++num4; } catch (Exception ex) { Area.logger.Error <Area, Exception>("Exception raised when processing TimerEntry in {0} : {1}.", this, ex); } } } stopwatch2.Stop(); num3 = stopwatch2.ElapsedMilliseconds; } finally { try { this.m_lastUpdateTime = now; ++this.TickCount; this.m_isUpdating = false; TimeSpan timeSpan = DateTime.Now - now; this.AverageUpdateTime = (float)(((double)this.AverageUpdateTime * 9.0 + timeSpan.TotalMilliseconds) / 10.0); Interlocked.Exchange(ref this.m_currentThreadId, 0); int millisecondsDelay = (int)((double)this.m_updateDelay - timeSpan.TotalMilliseconds); if (millisecondsDelay < 0) { millisecondsDelay = 0; Area.logger.Debug("Area '{0}' update lagged ({1}ms) (msg:{2}ms, timers:{3}ms, timerProc:{4}/{5})", (object)this, (object)(int)timeSpan.TotalMilliseconds, (object)num2, (object)num3, (object)num4, (object)this.m_timers.Count); } if (!this.IsRunning) { this.m_stoppedAsync.Set(); } TaskFactoryExtensions.StartNewDelayed(Task.Factory, millisecondsDelay, new Action <object>(this.UpdateCallback), (object)this); } catch (Exception ex) { Area.logger.Error <Area, Exception>("Area {0}. Could not recall callback !! Exception {1}", this, ex); } } }