/// <summary> /// Stops the current async search if running. /// </summary> public void StopAsync() { if (AsyncRunning) { mdDiscovery.Stop(); mdDiscovery.DeviceAdded -= new DeviceAddedEventHandler(mdDiscovery_DeviceAdded); mdDiscovery.DeviceRemoved -= new DeviceRemovedEventHandler(mdDiscovery_DeviceRemoved); mdDiscovery.SearchComplete -= new SearchCompleteEventHandler(mdDiscovery_SearchComplete); mdDiscovery.Dispose(); mdDiscovery = null; } }
/// <summary> /// Finds devices by device or service type synchronously using a timeout period. /// </summary> /// <param name="uriString">The URI string to search for or null / empty for all.</param> /// <param name="timeoutMS">The maximum timeout time in milliseconds to wait or -1 for wait forever.</param> /// <param name="maxDevices">The maximum number of devices to find before returning or 0 for as many as possible.</param> /// <param name="searchCompleted">True if the search completed and all available devices were returned.</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> public static Devices FindDevices( string uriString, int timeoutMS, int maxDevices, out bool searchCompleted, AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth, bool resolveNetworkInterfaces = false) { Discovery ldDiscovery = new Discovery(uriString, addressFamily, resolveNetworkInterfaces); Devices ldDevices = new Devices(); bool lbSearchCompleted = false; ManualResetEvent lmreComplete = new ManualResetEvent(false); ldDiscovery.DeviceAdded += (sender, args) => { ldDevices.Add(args.Device); if (maxDevices > 0 && ldDevices.Count >= maxDevices) { lmreComplete.Set(); } }; ldDiscovery.SearchComplete += (sender, args) => { lbSearchCompleted = true; lmreComplete.Set(); }; ldDiscovery.Start(); lmreComplete.WaitOne(timeoutMS); searchCompleted = lbSearchCompleted; ldDiscovery.Dispose(); return(ldDevices); }
/// <summary> /// Finds devices by device or service type synchronously using a timeout period. /// </summary> /// <param name="uriString">The URI string to search for or null / empty for all.</param> /// <param name="timeoutMS">The maximum timeout time in milliseconds to wait or -1 for wait forever.</param> /// <param name="maxDevices">The maximum number of devices to find before returning or 0 for as many as possible.</param> /// <param name="searchCompleted">True if the search completed and all available devices were returned.</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> public static Devices FindDevices( string uriString, int timeoutMS, int maxDevices, out bool searchCompleted, AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth, bool resolveNetworkInterfaces = false) { Discovery ldDiscovery = new Discovery(uriString, addressFamily, resolveNetworkInterfaces); Devices ldDevices = new Devices(); bool lbSearchCompleted = false; ManualResetEvent lmreComplete = new ManualResetEvent(false); ldDiscovery.DeviceAdded += (sender, args) => { ldDevices.Add(args.Device); if (maxDevices > 0 && ldDevices.Count >= maxDevices) lmreComplete.Set(); }; ldDiscovery.SearchComplete += (sender, args) => { lbSearchCompleted = true; lmreComplete.Set(); }; ldDiscovery.Start(); lmreComplete.WaitOne(timeoutMS); searchCompleted = lbSearchCompleted; ldDiscovery.Dispose(); return ldDevices; }