public void DeviceLocator_SearchAsync_HandlesConcurrentDispose() { using (var deviceLocator = new Rssdp.SsdpDeviceLocator()) { System.Threading.ThreadPool.QueueUserWorkItem((reserved) => { System.Threading.Thread.Sleep(50); deviceLocator.Dispose(); }); var t = deviceLocator.SearchAsync(); AggregateException exception = null; try { t.Wait(); } catch (AggregateException aex) { exception = aex; } Assert.AreNotEqual(null, exception); Assert.AreEqual(1, exception.InnerExceptions.Count); Assert.AreEqual(typeof(System.ObjectDisposedException), exception.InnerExceptions.First().GetType()); } }
public async Task StartDiscoveringAsync() { var runningTask = Task.Run(async () => { await Task.Delay(TimeSpan.MaxValue, _cancellationToken.Token).ContinueWith(_ => { }); }); await Task.Delay(TimeSpan.FromSeconds(5), _cancellationToken.Token).ContinueWith(_ => { }); using (var deviceLocator = new SsdpDeviceLocator()) { deviceLocator.StartListeningForNotifications(); deviceLocator.DeviceAvailable += async (s, e) => { await UpnpDeviceFound(e.DiscoveredDevice); }; while (!_cancellationToken.IsCancellationRequested) { Task<IEnumerable<DiscoveredSsdpDevice>> searchTask; try { searchTask = deviceLocator.SearchAsync("upnp:rootdevice", TimeSpan.FromSeconds(10)); await Task.WhenAny(runningTask, searchTask); } catch (Exception e) { _log.Error(e.Message, e); continue; } if (_cancellationToken.IsCancellationRequested) { break; } var devices = searchTask.Result; if (devices == null) { continue; } foreach (var device in devices) { await UpnpDeviceFound(device); } await Task.Delay(TimeSpan.FromSeconds(60), _cancellationToken.Token).ContinueWith(_ => { }); } } }
public async Task<ObservableCollection<Chromecast>> LocateDevicesAsync() { using (var deviceLocator = new SsdpDeviceLocator()) { var foundDevices = await deviceLocator.SearchAsync("urn:dial-multiscreen-org:device:dial:1", TimeSpan.FromMilliseconds(5000)); foreach (var foundDevice in foundDevices) { var fullDevice = await foundDevice.GetDeviceInfo(); Uri myUri; Uri.TryCreate("https://" + foundDevice.DescriptionLocation.Host, UriKind.Absolute, out myUri); var chromecast = new Chromecast { DeviceUri = myUri, FriendlyName = fullDevice.FriendlyName }; DiscoveredDevices.Add(chromecast); } } return DiscoveredDevices; }