/// <summary> /// Discover devices in LAN /// </summary> /// <returns></returns> public static async IAsyncEnumerable <Device> DiscoverAndEnumerateAsync(CancellationToken?cancel = null) { List <Task <IEnumerable <Device> > > tasks = NetworkInterface.GetAllNetworkInterfaces() .Where(networkInterface => networkInterface.OperationalStatus == OperationalStatus.Up) .Select(networkInterface => DeviceLocator.DiscoverAsync(networkInterface, deviceFoundReporter: null)).ToList(); Dictionary <string, Device> allUniqueDevices = new Dictionary <string, Device>(); while (tasks.Count != 0) { Task <IEnumerable <Device> > completedTask = await Task.WhenAny(tasks); tasks.Remove(completedTask); IEnumerable <Device> devices = await completedTask; foreach (Device device in devices) { if (!allUniqueDevices.ContainsKey(device.Hostname)) { allUniqueDevices.Add(device.Hostname, device); yield return(device); } } if (cancel.HasValue && cancel.Value.IsCancellationRequested) { break; } } }
/// <summary> /// Discover devices in a specific Network Interface /// </summary> /// <param name="networkInterface"></param> /// <returns></returns> public static async Task <IEnumerable <Device> > DiscoverAsync(NetworkInterface networkInterface) => await DeviceLocator.DiscoverAsync(networkInterface, null);
/// <summary> /// Discover devices in LAN /// </summary> /// <returns></returns> public static async Task <IEnumerable <Device> > DiscoverAsync(IProgress <Device> deviceFoundReporter) { IEnumerable <Task <IEnumerable <Device> > > tasks = NetworkInterface.GetAllNetworkInterfaces() .Where(networkInterface => networkInterface.OperationalStatus == OperationalStatus.Up) .Select(networkInterface => DeviceLocator.DiscoverAsync(networkInterface, deviceFoundReporter)); IEnumerable <Device>[] result = await Task.WhenAll(tasks); return(result .SelectMany(devices => devices) .GroupBy(d => d.Hostname) .Select(g => g.First()) .ToList()); }
/// <summary> /// Discover devices in LAN /// </summary> /// <returns></returns> public static async Task <IEnumerable <Device> > DiscoverAsync() => await DeviceLocator.DiscoverAsync(deviceFoundReporter : null);