/// <summary> /// Executes when a device is removed from enumeration /// </summary> /// <param name="sender">The device watcher.</param> /// <param name="deviceInfoUpdate">An update of the device.</param> private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1))) { var device = BluetoothLeDevices.FirstOrDefault(i => i.DeviceInfo.Id == deviceInfoUpdate.Id); BluetoothLeDevices.Remove(device); var unusedDevice = _unusedDevices.FirstOrDefault(i => i.Id == deviceInfoUpdate.Id); _unusedDevices?.Remove(unusedDevice); _readerWriterLockSlim.ExitWriteLock(); } }); }
/// <summary> /// Executes when a device is removed from enumeration /// </summary> /// <param name="sender">The device watcher.</param> /// <param name="deviceInfoUpdate">An update of the device.</param> private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == _deviceWatcher) { await DispatcherQueue.ExecuteOnUIThreadAsync( () => { if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1))) { var device = BluetoothLeDevices.FirstOrDefault(i => i.DeviceInfo.Id == deviceInfoUpdate.Id); BluetoothLeDevices.Remove(device); var unusedDevice = _unusedDevices.FirstOrDefault(i => i.Id == deviceInfoUpdate.Id); _unusedDevices?.Remove(unusedDevice); _readerWriterLockSlim.ExitWriteLock(); } }, DispatcherQueuePriority.Normal); } }