Reputation: 34297
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.
Upvotes: 0
Views: 1934
Reputation: 4742
Try this:
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, action);
You invoke your action synchronously and it causes UI to freeze.
Upvotes: 1