public int GetFreeRoomId() { bool reachedEnd = false; _roomIdSync.EnterWriteLock(); try { while (_idCache.ContainsKey(_lastFreeRoomId) || _overrideLoaders.ContainsKey(_lastFreeRoomId)) { if (_lastFreeRoomId == int.MinValue) { if (!reachedEnd) { _lastFreeRoomId = -1; reachedEnd = true; } else { throw new Exception("No spare negative RoomIDs!"); } } else { _lastFreeRoomId--; } } return(_lastFreeRoomId); } finally { _roomIdSync.ExitWriteLock(); } }