using System; using System.Collections.Generic; using System.Text; namespace Wayne.Lib { /// /// Handles a collection of IConnectable items, and checks them all so they are connected. /// public class ConnectionChecker { private readonly IList connectableItems = new List(); private readonly object connectableItemsLock=new object(); /// /// Is all connectable items connected? /// /// public bool IsConnectedToAll() { lock (connectableItemsLock) { foreach (var connectableItem in connectableItems) { if (connectableItem.Connectable.ConnectionState != DeviceConnectionState.Connected) return false; } } return true; } /// /// Adds a connectable entity to the check. /// /// /// public void AddConnectableToCheck(IConnectable connectable, string connectableDisplayName) { if(connectable == null) throw new ArgumentNullException("connectable", "A instance of IConnectable is required."); connectableItems.Add(new ConnectableItem() { Connectable = connectable, DisplayName = connectableDisplayName}); } /// /// Gets a list of strings containingt those items that are not connected, and their current connection state. /// /// public IList GetUnconnectedAsText() { IList reasons = new List(); foreach (var connectableItem in connectableItems) { if (connectableItem.Connectable.ConnectionState != DeviceConnectionState.Connected) reasons.Add(connectableItem.DisplayName + ".ConnectionState = " + connectableItem.Connectable.ConnectionState); } return reasons; } class ConnectableItem { public IConnectable Connectable { get; set; } public string DisplayName { get; set; } } } }