Esempio n. 1
0
        /// <summary>
        /// Finds devices by device or service type asynchronously using a timeout period.
        /// </summary>
        /// <param name="uriString">The URI string to search for or null for all devices.</param>
        /// <param name="timeoutMS">The maximum timeout time in milliseconds to wait.</param>
        /// <param name="maxDevices">The maximum number of devices to find before returning.</param>
        /// <param name="devicesFound">
        /// The delegate to call when async operation is complete. <para /> NOTE: this delegate is executed
        /// in a different to the calling thread.</param>
        /// <param name="addressFamily">The address family to search in. (Vista or above only).</param>
        /// <param name="resolveNetworkInterfaces">True to resolve network interface Guids.</param>
        /// <returns>A Devices list containing the found devices.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when devicesFound delegate is null.</exception>
        public static void FindDevicesAsync(
            string uriString,
            int timeoutMS,
            int maxDevices,
            DevicesFoundDelegate devicesFound,
            AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth,
            bool resolveNetworkInterfaces    = false)
        {
            if (devicesFound == null)
            {
                throw new ArgumentNullException("devicesFound");
            }

            Object[] loParams = new object[]
            {
                uriString, timeoutMS, maxDevices,
                addressFamily, resolveNetworkInterfaces,
                devicesFound
            };

            Devices ldDevices         = null;
            bool    lbSearchCompleted = false;
            Thread  ltCurrent         = Thread.CurrentThread;

            Thread ltThread = new Thread(
                (object args) =>
            {
                try
                {
                    object[] loArgs = (object[])args;

                    ldDevices = Discovery.FindDevices(
                        (string)loArgs[0], (int)loArgs[1], (int)loArgs[2],
                        out lbSearchCompleted, (AddressFamilyFlags)loArgs[3],
                        (bool)loArgs[4]);
                }
                finally
                {
                    devicesFound(ldDevices, lbSearchCompleted);
                }
            }
                );

            ltThread.Start(loParams);
        }
Esempio n. 2
0
        /// <summary>
        /// Finds devices by device or service type asynchronously using a timeout period.
        /// </summary>
        /// <param name="uriString">The URI string to search for or null for all devices.</param>
        /// <param name="timeoutMS">The maximum timeout time in milliseconds to wait.</param>
        /// <param name="maxDevices">The maximum number of devices to find before returning.</param>
        /// <param name="devicesFound">
        /// The delegate to call when async operation is complete. <para /> NOTE: this delegate is executed
        /// in a different to the calling thread.</param>
        /// <param name="addressFamily">The address family to search in. (Vista or above only).</param>
        /// <param name="resolveNetworkInterfaces">True to resolve network interface Guids.</param>
        /// <returns>A Devices list containing the found devices.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when devicesFound delegate is null.</exception>
        public static void FindDevicesAsync(
            string uriString,
            int timeoutMS,
            int maxDevices,
            DevicesFoundDelegate devicesFound,
            AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth,
            bool resolveNetworkInterfaces = false)
        {
            if(devicesFound == null) throw new ArgumentNullException("devicesFound");

            Object[] loParams = new object[]
            {
                uriString, timeoutMS, maxDevices,
                addressFamily, resolveNetworkInterfaces,
                devicesFound
            };

            Devices ldDevices = null;
            bool lbSearchCompleted = false;
            Thread ltCurrent = Thread.CurrentThread;

            Thread ltThread = new Thread(
                (object args) =>
                    {
                        try
                        {
                            object[] loArgs = (object[])args;

                            ldDevices = Discovery.FindDevices(
                                (string)loArgs[0], (int)loArgs[1], (int)loArgs[2],
                                out lbSearchCompleted, (AddressFamilyFlags)loArgs[3],
                                (bool)loArgs[4]);
                        }
                        finally
                        {
                            devicesFound(ldDevices, lbSearchCompleted);
                        }
                    }
            );

            ltThread.Start(loParams);
        }