How do I update an ObservableCollection via a worker thread?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If an ObservableCollection is bound to WPF UI elements, you generally cannot modify it directly from a worker thread. The collection change notifications are consumed by UI components that belong to the dispatcher thread, so cross-thread updates usually throw exceptions or lead to unstable behavior.
The safe pattern is to do expensive work in the background, then marshal only the collection mutation back to the UI thread. That keeps the interface responsive without violating WPF's thread-affinity rules.
Why Direct Background Updates Fail
A bound ObservableCollection raises CollectionChanged events. WPF expects those notifications to occur on the dispatcher thread that owns the bound controls.
This is why code like this is unsafe:
Even if it seems to work sometimes, it is not a valid threading model for a bound collection.
Use the Dispatcher for Collection Mutation
A common approach is to fetch or compute data on a background thread, then update the collection on the UI thread.
This is the normal WPF pattern. The background task handles slow work, and the dispatcher handles the UI-facing mutation.
Batch Updates Are Better Than Many Tiny Dispatcher Calls
A common mistake is calling Dispatcher.Invoke once per item inside a loop. That works, but it produces unnecessary UI-thread churn.
This is worse:
This is better:
Move the whole batch to the dispatcher in one operation when possible.
Replacing the Collection Can Also Be Useful
If the refresh is effectively "load a new data set," you may prefer building a new collection off-thread and swapping the bound property on the UI thread.
Then assign the new collection on the dispatcher thread once the background work completes. This can be cleaner than many incremental mutations for full refresh scenarios.
EnableCollectionSynchronization Is a Special Tool
WPF also provides BindingOperations.EnableCollectionSynchronization, which lets binding code coordinate access with a lock.
This can help in advanced multi-threaded scenarios, but it is not the first tool to reach for. Dispatcher-based updates are usually simpler and easier to reason about.
Keep Background Work Separate from UI Work
The real design rule is separation of concerns:
- do network, disk, or CPU-heavy work in the background
- do collection mutation on the dispatcher
- keep the dispatcher block as small as possible
If you put slow work inside Dispatcher.Invoke, you lose the main benefit of using a worker thread in the first place.
Common Pitfalls
- Adding or removing items from a bound
ObservableCollectiondirectly on a worker thread. - Calling the dispatcher once per item instead of batching UI updates.
- Performing expensive work inside
Dispatcher.Invoke, which blocks the UI. - Treating
EnableCollectionSynchronizationas a blanket replacement for careful UI-thread design. - Forgetting cancellation and error handling in long-running background refresh logic.
Summary
- A UI-bound
ObservableCollectionshould usually be modified on the WPF dispatcher thread. - Run heavy work in the background and marshal only the collection mutation back to the UI thread.
- Batch updates to reduce dispatcher overhead.
- Consider replacing the whole collection for full refresh scenarios.
- Use
EnableCollectionSynchronizationonly when you truly need shared multi-threaded access.
Related reading
- How do I update the GUI from another thread?
- How do I update the GUI from another thread?
- How do I use await inside a generator?
- How do I use threading in Python?
- How do I upload a file to an SFTP server in C .NET?
- How do I use a C keyword as a property name?
- How do I use threading in Python?
- How do I use threading in Python?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.