public void RelayMessage(ChatService current, string value) { List<ChatService> defunct = null; this.thisLock.EnterReadLock(); try { foreach (var entry in this.innerCache) { try { entry.SendMessage(value); } catch { if (defunct == null) { defunct = new List<ChatService>(); } defunct.Add(entry); } } } finally { this.thisLock.ExitReadLock(); } if (defunct != null) { this.thisLock.EnterWriteLock(); try { foreach (var entry in defunct) { this.innerCache.Remove(entry); } } finally { this.thisLock.ExitWriteLock(); } } }
/// <summary> /// Attempting to remove a session from the collection. /// </summary> /// <param name="entry">Session to remove.</param> public void Remove(ChatService entry) { ThreadPool.QueueUserWorkItem(new WaitCallback(this.RemoveInternal), entry); }
/// <summary> /// Attempting to add another session to the collection. /// </summary> /// <param name="entry">Session to add.</param> /// <returns>true if session was added; false otherwise.</returns> public bool TryAdd(ChatService entry) { this.thisLock.EnterUpgradeableReadLock(); try { if (this.innerCache.Contains(entry)) { return false; } this.thisLock.EnterWriteLock(); try { this.innerCache.Add(entry); return true; } finally { this.thisLock.ExitWriteLock(); } } finally { this.thisLock.ExitUpgradeableReadLock(); } }