One way I have figured out is to store the BackgroundWorkers in a
collection and keep checking their IsBusy property. I am still not sure
if this is the best way to do it. I would really appreciate some help.

Your loop checking the BackgroundWorkers' IsBusy property wil burn
away CPU cycles without doing anything productive (it will effectively
steal CPU time that your other threads would need to perform their
operations). You could improve this by including calls to Thread.Sleep
in your code, making the main thread only check at specified
intervals. However, it would still not be very elegant.

The point is that BackgroundWorker is not really just for executing
operations on a background thread, it's rather for doing this _while
keeping the user interface responsive_. If you do not have a UI, the
preferred way to execute operations on a background thread without
explicit thread management would rather be to use
ThreadPool.QueueUserWorkItem.

To synchronize in combination with QueueUserWorkItem, you can use
WaitHandles. Greg Young has provided two links that would probably be
useful for your scenario. Especially take a look at the sample in the
MSDN topic he linked; it seems to be quite similar to your
requirement.

Fabian

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to