WPF: Modifying CollectionView from Dispatcher still throws errors
- by Dusda
I have the following bit of code that modifies an observable collection of 'screens' whenever a user leaves.
void OnUserLeft(int roomId, int userId, string username)
{
client.ClientDispatcher.Invoke(
(Action<int>)((id) =>
{
Console.WriteLine("Hello before the storm!");
var screensToCheck = client.Screens.Where(s => s.CpuId == id).ToList();
screensToCheck.Each(s => client.Screens.Remove(s));
Console.WriteLine("Hello there!");
}), userId);
}
This is wrapped in a call to the client's Dispatcher, supposedly to get past the threading issues related to CollectionViews. However, I still get the following exception:
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
The Dispatcher you see above is set in the WPF application's MainViewModel (we're using MVVM), like so:
public Dispatcher ClientDispatcher
{
get { return Dispatcher.CurrentDispatcher; }
}