views:

33

answers:

1

I'm writing a plug-in for a program where I have a custom user control with a TreeView. When the user selects or deselects objects in the native program ObjectSelected and ObjectDeselected events are evoked. When these events are fired my program will select or deselect corresponding nodes in the tree view.

The problem is this is a graphic 3d modeling program. So it's really easy for the user to draw a selection box across a thousand little objects and pummel my code with a thousand ObjectSelected events. So right now my plug-in works but if the user selects too many objects they have to sit there an wait for my plugin to deal with all the ObjectSelected events.

Instead of doing something at each selection event I could instead cache the selected objects into a separate collection and then process the list in bulk rather then each object one at a time. But how can I tell when the last ObjectSelected event is evoked?

I think I need to utilize multiple threads so that each object selected event resets a timer. When the timer finally gets a chance to reach it's end time it processes the full list of selected objects.

Although I've never done anything with multiple threads so I have no idea where to start on something like that. Plus I don't really like the idea of having my plug-in rely on some predefined time limit. What if the a user is on a faster/slower computer? Or what if one of the objects they are selecting is really complex and takes longer to select for some reason then other objects?

A: 

Although you said you don't want to work with multiple threads, this is the exact situation for them (having a UI on a seperate thread as the background calculations).

Luckily, C# makes basic threading really easy. Have a look at the BackgroundWorker class and some of the examples with it.

You can fix the issues with selecting diffrent items rapidly by adding the ability to cancel the BackgroundWorker and start over with the new calculations.