Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an ObservableCollection<T> with Background Threads

It seems like Microsoft had a great idea with the ObservableCollection. They are great for binding, and are super fast on the UI.

However, requiring a context switch to the Dispatcher Thread every time you want to tweak it seems like a bit much. Does anyone know the best practices for using them? Is it simply to populate an ICollection as a message object in the business layer, then create the ObservableCollection in the UI layer? How do you then handle updates to the collection on the UI?

like image 880
vt100 Avatar asked Oct 13 '08 20:10

vt100


2 Answers

Is updating the ObservableCollection on the UI thread really causing that much of a bottleneck for your application? If not, stick with updating it on the UI thread. Remember, it's not really a context switch that's happening when you run something with the Dispatcher - instead, you're simply submitting a job to the UI thread, which is an already running thread, which the OS will context switch to at some point anyway. The UI thread pulls your submitted job off of an internal queue and executes it. You're not forcing the context switch yourself.

like image 124
Rob Avatar answered Oct 06 '22 21:10

Rob


You can use good old BackgroundWorker also in WPF (as in Windows Forms). It will adopt to the threading model of WPF and also provide a nice abstraction.

like image 45
huseyint Avatar answered Oct 06 '22 21:10

huseyint