Search code examples
c#.netmultithreadingdesign-decisions

When should I not use the ThreadPool in .Net?


When should I not use the ThreadPool in .Net?

It looks like the best option is to use a ThreadPool, in which case, why is it not the only option?

What are your experiences around this?


Solution

  • The only reason why I wouldn't use the ThreadPool for cheap multithreading is if I need to…

    1. interract with the method running (e.g., to kill it)
    2. run code on a STA thread (this happened to me)
    3. keep the thread alive after my application has died (ThreadPool threads are background threads)
    4. in case I need to change the priority of the Thread. We can not change priority of threads in ThreadPool which is by default Normal.

    P.S.: The MSDN article "The Managed Thread Pool" contains a section titled, "When Not to Use Thread Pool Threads", with a very similar but slightly more complete list of possible reasons for not using the thread pool.

    There are lots of reasons why you would need to skip the ThreadPool, but if you don't know them then the ThreadPool should be good enough for you.

    Alternatively, look at the new Parallel Extensions Framework, which has some neat stuff in there that may suit your needs without having to use the ThreadPool.