Just wondered what that best approach is for this scenario - trying to databind to a collection which is being populated in another background thread.
My background thread is adding items to the collection in a processing loop which may run for several minutes. Every now and then it raises an event to the UI and passes a reference to the data from the collection, for the UI to visualize, (so the user can start to interact with what they have) and carries on processing.
Trouble is the UI starts to render the visualisation (which is quite complex itself), which involves a foreach() loop over the collection of data, and understandably that loop craps itself if my background thread changes the data in the collection during the enumeration.
So my brainstorming has gone like this:
- pause the background thread; but I really don’t want to pause
- take a duplicate snapshot copy of all, or some the data in each event, and databind to the snapshot. Doubles my memory usage but would probably work
- implement some kind of lock{} on the syncroot or whatever of the collection so while the UI is updating the background process has to wait. Not confident about that working anyway
- fire the event all the time and just pass one bit of data at a time, which has the same result as #2 but with more overhead..
cheers ewart.