/// <summary> /// Calls a delegate on all connected devices. This re-evaluates the currently connected devices /// after each go, so if some devices take a while to enumerate, they will probably still be caught /// after the first device work finishes (since the work is typically long). /// </summary> /// <param name="DelayEnumerationPeriodMS">The initial number of ms to wait after the first enumerated device is found</param> /// <param name="PerDeviceWork">The delegate to perform on each connected device</param> /// <returns>True if all devices had the work successfully performed, and false if any failed or none were found</returns> bool PerformActionOnAllDevices(uint DelayEnumerationPeriodMS, PerformDeviceActionDelegate PerDeviceWork) { // Start enumerating the devices ConnectToDevices(DelayEnumerationPeriodMS); // Keep looking at the device list and executing on new ones as long as we keep finding them Dictionary <string, bool> Runs = new Dictionary <string, bool>(); bool bFoundNewDevices = true; while (bFoundNewDevices) { IEnumerable <MobileDeviceInstance> Devices = MobileDeviceInstanceManager.GetSnapshotInstanceList(); bFoundNewDevices = false; foreach (MobileDeviceInstance PotentialDevice in Devices) { PotentialDevice.Reconnect(); string DeviceId = PotentialDevice.DeviceId; if (DeviceId == String.Empty) { System.Threading.Thread.Sleep((int)SleepAfterFailedDeviceCallMS); DeviceId = PotentialDevice.DeviceId; } if (!Runs.ContainsKey(DeviceId) && PotentialDevice.IsConnected) { // New device, do the work on it Runs.Add(DeviceId, PerDeviceWork(PotentialDevice)); bFoundNewDevices = true; } } } // Determine if all succeeded bool bAllSucceeded = true; bool bAnySucceeded = Runs.Count > 0; foreach (var KVP in Runs) { bAllSucceeded = bAllSucceeded && KVP.Value; } return(bAllSucceeded && bAnySucceeded); }
/// <summary> /// Tries to connect to all devices /// </summary> /// <param name="DelayAfterFirstDeviceMS"></param> void ConnectToDevices(uint DelayAfterFirstDeviceMS) { { ReportIF.Log("Trying to connect to mobile device running iOS ..."); try { // Initialize the mobile device manager if (!bHaveRegisteredHandlers) { Manzana.MobileDeviceInstanceManager.Initialize(MobileDeviceConnected, MobileDeviceDisconnected); bHaveRegisteredHandlers = true; } // Wait for connections to roll in int SleepDurationMS = 100; int TotalDurationMS = 5000; while (!MobileDeviceInstanceManager.AreAnyDevicesConnected() && (TotalDurationMS > 0)) { System.Threading.Thread.Sleep(SleepDurationMS); TotalDurationMS -= SleepDurationMS; } // Wait one additional tick in case any new devices come online //@TODO: Is there a better way to determine if all devices have been enumerated? System.Threading.Thread.Sleep((int)DelayAfterFirstDeviceMS); if (!MobileDeviceInstanceManager.AreAnyDevicesConnected()) { ReportIF.Error("Timed out while trying to connect to a mobile device. Make sure one is connected."); } } catch (ThreadAbortException) { // we expect this to happen so we don't log it } catch (Exception ex) { ReportIF.Error(String.Format("Error encountered ('{0}') while trying to connect to a mobile device. Please verify that iTunes is installed", ex.Message)); } } }