Use the Dispatcher properly.
You have to add things to your ListBox (or its data source) from the Dispatcher thread, else the UI will explode. When you do this, you're either in a method on that thread already or (hopefully more likely) you're in a background thread and using Dispatcher.BeginInvoke
to add the item.
When you kick the update across to the Dispatcher, use Dispatcherpriority.Normal
as the priority. The Dispatcher has a queue of work items and your Normal
items will push in (perhaps surprising) high in that queue.
The bit of code that WPF runs to update a data binding when the underlying data has changed runs at DispatcherPriority.DataBind
, which is lower than Normal
. That means the data binding won't typically be updated until all of your items are added (or alternatively, if your items take a long time to add, it might happen when the Dispatcher is idle between adding items).
The bit of code that actually renders your control (say, when its binding signals an update) runs at DispatcherPriority.Render
and is even lower priority than the binding. That means that your control will only render when the Dispatcher has run out of bindings to update, which in turn will only happen when the Dispatcher has run out of item-adds to process.
If this sounds strange, remember that each layer (update - binding - render) causes a metaphorical flag to be set on the layer below - you don't get ten bindings and then ten renders happening. If your item adds are fast, you'll get all your adds followed by one bind and one render - which is perfect.
Basically: if you use the Dispatcher as its intended you'll have nothing to worry about. It seems "wrong" to have render as a relatively low priority on the Dispatcher but, actually, it's very clever :-)