0

When updating my ObservableCollection, I was getting this error:

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

Using this answer as a guide, I thought this code would work:

private ObservableCollection<string> _userMessages = new ObservableCollection<string>();

public void AddUserMessage(string message)
{
    lock (_locker)
    {
        Action action = () =>
        {
            this._userMessages.Add(message);
        };

        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, action);
    }
}

However, my UI now freezes when calling Dispatcher.Invoke(). What am I doing incorrectly?

Note: I needed to do this because I'm (sometimes) updating my ObservableCollection from events.

Community
  • 1
  • 1
Bob Horn
  • 33,387
  • 34
  • 113
  • 219

1 Answers1

1

Try this:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, action);

You invoke your action synchronously and it causes UI to freeze.

Vyacheslav Volkov
  • 4,592
  • 1
  • 20
  • 20