/// <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);
        }
Example #2
0
        /// <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<MobileDeviceInstance, bool> Runs = new Dictionary<MobileDeviceInstance, bool>();
            bool bFoundNewDevices = true;
            while (bFoundNewDevices)
            {
                IEnumerable<MobileDeviceInstance> Devices = MobileDeviceInstanceManager.GetSnapshotInstanceList();

                bFoundNewDevices = false;
                foreach (MobileDeviceInstance PotentialDevice in Devices)
                {
                    if (!Runs.ContainsKey(PotentialDevice))
                    {
                        string DeviceName = PotentialDevice.DeviceName;
                        if (DeviceName == String.Empty)
                        {
                            System.Threading.Thread.Sleep((int)SleepAfterFailedDeviceCallMS);
                        }

                        // New device, do the work on it
                        Runs.Add(PotentialDevice, 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;
        }