WPF: Modifying CollectionView from Dispatcher still throws errors

Posted by Dusda on Stack Overflow See other posts from Stack Overflow or by Dusda
Published on 2010-05-13T21:45:41Z Indexed on 2010/05/13 21:54 UTC
Read the original article Hit count: 626

Filed under:
|
|

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; }
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about dispatcher