TimeSpan rxTimeout = new TimeSpan(0, 0, 20); // twenty seconds public CoordinatorConnection GetConnection(string theHostAddress) { CoordinatorConnection coordinator; Monitor.Enter(this); // don't let other threads alter the pool while we're working on it if (pool.Contains(theHostAddress)) { coordinator = pool[theHostAddress] as CoordinatorConnection; } else { coordinator = new CoordinatorConnection(theHostAddress, rxTimeout); pool.Add(theHostAddress, coordinator); } Monitor.Exit(this); // let other threads in return(coordinator); }
private void DoRemoveIdleConnections() { // Removing items from a collection during iteration // is not allowed, so we create a clone of the original // collection. We then iterate over the clone, but // remove items from the original collection Hashtable clonedHashtable = pool.Clone() as Hashtable; foreach (string hostAddress in clonedHashtable.Keys) { CoordinatorConnection coordinator = clonedHashtable[hostAddress] as CoordinatorConnection; if (coordinator.IsIdle) { pool.Remove(hostAddress); coordinator.Disconnect(); } } }