何时不应使用.NET中的线程池? [英] When should I not use the ThreadPool in .Net?

查看:199
本文介绍了何时不应使用.NET中的线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

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

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

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

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

另外,看看新的<一个href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e848dc1d-5be3-4941-8705-024bc7f180ba">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…

  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.

这篇关于何时不应使用.NET中的线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆