// PRIVATE STATIC // The idea is that the thread will sleep based on the shortest timespan for the session in place private static void ExpireIdleSessions() { TimeSpan timeout = SessionManager.sessionMaxIdleTime; while (sessionThreadFlag) { foreach (string key in SessionManager.sessionsBag.Keys) { SessionContainer sessionContainer = (SessionContainer)SessionManager.sessionsBag[key]; if (sessionContainer.NeedExpireSession(SessionManager.sessionMaxIdleTime)) { if (!SessionManager.sessionsBag.TryRemove(key, out SessionContainer container)) { Console.WriteLine("On ExpireIdleSessions - Could not remove SessionContainer from sessions bag - sessionId=[" + key + "]"); } if (SessionManager.sessionsBag.Count == 0) { SessionManager.sessionThreadFlag = false; } } else { TimeSpan expirationTime = sessionContainer.GetExpirationDurationInMinutes(SessionManager.sessionMaxIdleTime); if (expirationTime.CompareTo(timeout) < 0) { timeout = expirationTime; } } } Thread.Sleep(timeout); } }