什么时候不应该在.Net中使用ThreadPool? [英] When should I not use the ThreadPool in .Net?

查看:156
本文介绍了什么时候不应该在.Net中使用ThreadPool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候应该使用.Net中的ThreadPool?



看起来最好的选择是使用ThreadPool,其中为什么不是唯一的选择?



你有什么经验?

解决方案

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



$ b
  • 与运行的方法(例如,杀死它)相结合

  • STA thread (这恰好是我的应用程序已经死了( ThreadPool 线程是后台线程)


  • ,以防我需要更改线程的优先级。我们不能改变ThreadPool中的线程的优先级,默认是Normal。




  • 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…

    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中使用ThreadPool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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