Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my View I have a chart which has a series that is bound to an ObservableCollection that is exposed in the ViewModel. There is a method in the ViewModel which is very computationally intensive and that method updates the values within the ObservableCollection that the series is bound to. The method takes a long time to complete so I launch it from a separate thread.

The method takes a long time to complete but is changing the parameters of the objects in the ObservableCollection very quickly and frequently. Because of this I am experiencing a lag in the UI similar to as if I was running the method on the UI thread.

Is there a way to limit how frequently the UI attempts to update the chart whenever an element within the binding gets changed? Or is it the job of the ViewModel to be aware of this potential problem and execute changes to the ObservableCollection intermittently - say with a timer which checks to make sure at least 150ms has passed since an element within the ObservableCollection has been updated?
Posted
Comments
[no name] 19-Feb-15 16:53pm    
Is the problem inside the objects inside the collection, or is it that you are changing the objects contained in the observable collection (e.g doing an add/replace/insert) ?
MrGlass3 19-Feb-15 16:54pm    
I am changing properties of the objects within the collection. There are no objects being added or removed from the collection.

1 solution

The best way would be to maintain a Dictionary of objects that have been poked since refresh, and rather than raising a PropertyChanged event, add to that dictionary if the entry does not exist...

Then create a behavior attached to the Items control that has the ItemsSource {Binding <myobservablecollection>}.

In this behavior, create a dispatcher timer, and on tick, loop through the Dictionary, and raise property changed on those objects. Then remove the entry from the dictionary.

If you are on WPF I would recommend that the dictionary uses ConcurrentDictionary<whateverworksasakey,weakreference><myviewmodel>>

Else you are going to have to wrap a lock around the contains/add/remove logic, which in your case might not be a good idea as its high frequency.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900