views:

204

answers:

2

If i am not wrong, Backgroundworker in .NET will get a thread from CLR ThreadPool, in order to perform background task. I would like to instantiate a collection or list of backgroundworker objects. However, not all the objects in the collection will be invoked in the sametime. So what worry me is that if i have a collection of 10 backgroundworkers objects, CLR ThreadPool will allocate 10 threads for me, and i think it will use up the threads in the ThreadPool if i have too many objects in my collection.

So here my question, when will ThreadPool allocates a thread to an instance of BackgroundWorker? Is it when the object is instantiated or when the method RunWorkerAsync() is called?

Thanks in advance for all of your help

A: 

This is an implementation detail you should not really be concerned about. Do not optimize nether perssimize prematurely. That is, do not create unnecessary instances of BackgroundWorkers and do not invent complex optimization logic whereby you'll be able to reuse those. Just instantiate them as need arises.

Anton Gogolev
A: 

As an answer to your question, the BackgroundWorker thread will be allocated by the ThreadPool in the RunWorkerAsync. You can study the next BackgroundWorker implementation to understand more of its mechanism.

BackgroundWorker implementation -in C#