I have a standard Listbox which is bound to a property in my viewmodel
<ListBox ItemsSource="{Binding StatusList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="myListBox" BorderThickness="0" HorizontalAlignment="Stretch">
</ListBox>
The property
private ObservableCollection<String> _statusList;
public ObservableCollection<String> StatusList
{
get { return _statusList;}
set { _statusList = value;}
}
The view model subscribes to an event
_eventAggregator.GetEvent<PublishStatusEvent>().Subscribe(this.OnStatusChanged);
which excecutes a function that just adds strings to the collection
private void OnStatusChanged(string status)
{
StatusList.Add(status);
}
When i exceute a long running task that publishes events , i want the listbox to update. If i debug i can see the events coming but the listbox is not getting updated until the task is finished. The task is inititated in the viewmodel.
Anyone?
I’m guessing that your ‘long running task’ is actually running on the UI thread and therefore blocking the UI thread even though you’re successfully publishing and subscribing events. This would explain why the events all appear when the task completes.
Try moving your task to another thread, maybe something like this:
You will then need to change your subscription code, as suggested by @shriek, to
The fact that you hadn’t specified
ThreadOption.UIThreadand weren’t getting a thread exception when adding an item to the status list also indicates that your task is currently on the UI thread.