Making sure OnPropertyChanged() is called on UI thread in MVVM WPF app

WPF automatically marshals property changes to the UI thread. However, it does not marshal collection changes, so I suspect your adding a message is causing the failure.

You can marshal the add manually yourself (see example below), or use something like this technique I blogged about a while back.

Manually marshalling:

public void backgroundWorker_ReportProgress(object sender, ReportProgressArgs e)
{
    Dispatcher.Invoke(new Action<string>(AddMessage), e.Message);
    OnPropertyChanged("Messages");
}

private void AddMessage(string message)
{
    Dispatcher.VerifyAccess();
    Messages.Add(message);
}

Leave a Comment