何时不应使用.NET中的线程池?不应、线程、NET

2023-09-02 20:48:43 作者:再撸剁手i

我要当不可以使用线程池在.net中?

它看起来像最好的选择是使用一个线程池,在这种情况下,为什么它不是唯一的选择?

什么是您的经验解决此问题?

解决方案

为什么我不会用的唯一原因线程池便宜的多线程是,如果我需要…

interract与运行(例如,将其杀死)的方法 运行$ C $在 STA线程 C(这发生在我身上) 保持线程活着后,我的申请已去世(线程池线程后台线程) 在情况下,我需要改变线程的优先级。我们不能在线程池默认情况下是正常更改线程的优先级。   

PS: MSDN文章的托管线程池 的包含一个章节,何时不使用线程池线程的,有一个非常相似,但略有更完整的可能原因不使用线程池列表。

有很多原因,你需要跳过线程池,但如果你不知道他们那么线程池应该是配不上你。

另外,看看新的Parallel扩展框架,其中有一些不错的东西在里面,可能会满足您的需求,而无需使用线程池

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.