The following code gives me an exception:
var snapshot = BluetoothCapture.Instance.Snapshot();
var allowedDevice = snapshot.FirstOrDefault( _ => some_expression );
Collection was modified; enumeration operation may not execute.
I thought I could use a lock to freeze the collection so that I can iterate through it. However, I'm still getting the same exception.
The class definition below has a Snapshot method that attempts this:
public partial class BluetoothCapture
{
...
public void Capture()
{
_watcher = DeviceInformation.CreateWatcher();
_watcher.Added += (s, e) => { _devices.Add(e); };
_watcher.Start();
}
public IEnumerable<DeviceInformation> Snapshot()
{
lock (_devices)
{
return _devices.AsReadOnly();
}
}
}
Any suggestions?