什么时候不应该在.Net中使用ThreadPool? [英] When should I not use the ThreadPool in .Net?
问题描述
什么时候应该不使用.Net中的ThreadPool?
看起来最好的选择是使用ThreadPool,其中为什么不是唯一的选择?
你有什么经验?
为便宜的多线程,我不会使用 ThreadPool
的唯一原因是如果我需要…
$ b
ThreadPool
线程是后台线程)
PS: MSDN文章 受管理的线程池 包含一个标题为不使用线程池线程的部分,具有非常相似但稍微更完整的列表可能的原因不使用线程池。
有很多理由为什么你需要跳过 ThreadPool
,但如果您不了解它们,那么 ThreadPool
应该对你来说足够好。
或者,查看新的并行扩展框架,它有一些整齐的东西,可以满足您的需要没有必须使用 ThreadPool
。
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?
The only reason why I wouldn't use the ThreadPool
for cheap multithreading is if I need to…
- interract with the method running (e.g., to kill it)
- run code on a STA thread (this happened to me)
- keep the thread alive after my application has died (
ThreadPool
threads are background threads) - 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
.
这篇关于什么时候不应该在.Net中使用ThreadPool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!