What thread does BackgroundWorker use/create

During my presentation on BackgroundWorker I got a smart and obvious question: What kind of thread does BackgroundWorker create or use. I saw this coming it is just I haven’t had time to check it out before the presentation. This was first thing I did once I got home. I fired up Reflector (as usual) and examined the inside of BackgroundWorker. My guess was that BackgroundWorker is creating a worker thread internally. Since the long operations are rare and one needs to run them asap this would be the behavior I expect. However, this is not the case. It is using a delegate and its BeginInvoke method internally. This makes BackgroundWorker depending on the ThreadPool. ThreadPool worker thread pool size is by default 25 on my machine and I assume it is more than enough for most of the WinForms applications so you don’t need to worry about. But if you run out of available threads BackgroundWorker won’t start the operation until a thread is available again. If you have this situation then you have three options:




  1. leave it as is – the user will just have to wait a bit more


  2. increase max thread pool size (ThreadPool.SetMaxThreads static method).


  3. use a worker thread directly instead of BackgroundWorker

It is up to you which path you’ll go in this rare, but possible situation.

Leave a Reply