/// <summary> /// Fires the Channel Pool Collected event after the timer fires and a pool cleanup/collection /// has occured. /// </summary> /// <param name="cpea"></param> protected void OnChannelPoolCollected(ChannelPoolCollectedEventArgs cpea) { if (ChannelPoolCollected != null) { ChannelPoolCollected(this, cpea); } }
/// <summary> /// The callback method that scans the pool for any end-of-life channels and purges them /// from the pool. This routine does not hold any locks so has to be pretty resilient about /// accessing and disposing of the channels which may well be used or removed at the time the /// cleanup occurs. /// </summary> //private void PoolCleanup(object sender, ElapsedEventArgs e) private void PoolCleanup() { Debug.WriteLine("in PoolCleanup event...."); while (_runCleanupTask) { Thread.Sleep(Config.CleanupInterval * 1000); int currentChannelCount = 0; lock (_repopulateThread) { DebugMessage("Cleanup process is checking pool status"); if (_channelList.Count > 0) { int latestCount = _channelList.Count - 1; for (int cnt = latestCount; cnt >= 0; cnt--) { try { ChannelContext <TChannel> ch = _channelList[cnt]; if (IsChannelExpired(ch)) { DebugMessage( string.Format( "channel found that needs to be removed. ChannelOpened:{0}, Current Time:{1}", ch.DateTimeOpened.ToShortTimeString(), DateTime.Now.ToShortTimeString())); // needs to be purged. try { _channelList.RemoveAt(cnt); currentChannelCount++; if (((ICommunicationObject)ch.Channel).State == CommunicationState.Opened || ((ICommunicationObject)ch.Channel).State == CommunicationState.Created) { ((ICommunicationObject)ch.Channel).Close(); } } catch (CommunicationException ce) { // do nothing here as well, the channel was already in a faulted state. DebugMessage("Channel to be removed was already in a faulted state"); } catch (Exception ex) { // To get here means the number of channels in the pool was exhaused in between accessing the // initial element, so we leave things as they are and let the asynch process refill the pool. DebugMessage("Unable to remove channel. [" + ex.Message + "]"); } } } catch { // and again, we dont care if we are out of range continue; } } } } // Fire the ChannelPoolCollected event. ChannelPoolCollectedEventArgs cpea = new ChannelPoolCollectedEventArgs(currentChannelCount); OnChannelPoolCollected(cpea); } }