简体   繁体   English

使用准则:C#中的ThreadPool.SetMaxThreads和ThreadPool.SetMinThreads

[英]Guidelines while using: ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads in C#

In one of my applications, I have to use multiple threads. 在我的一个应用程序中,我必须使用多个线程。 As a better approach, I have replaced a thread queue by ThreadPool . 作为更好的方法,我用ThreadPool代替了线程队列。

At start of Form, I set Min/Max Threads as follows: 在窗体开始时,我将最小/最大线程数设置如下:

  ThreadPool.SetMaxThreads(20,20)
  ThreadPool.SetMinThreads(1,1)

Later on while using, I use ThreadPool as follow: 稍后在使用时,我按以下方式使用ThreadPool

 Function()
 {
       ThreadPool.QueueUserWorkItem(new WaitCallback(Action), arguments); 
 }

I am not using any sort of DeQueue . 我没有使用任何DeQueue

It will be help full if some one can share their experience with ThreadPool (specially Set MIN/MAX interfaces) 如果有人可以分享他们对ThreadPool的经验(特别是设置MIN / MAX接口),将完全有帮助。

Regards, Sachin 问候,萨钦

Generally, it's not recommended to change the thread pool size. 通常,不建议更改线程池大小。 From MSDN: 从MSDN:

Use caution when changing the maximum number of threads in the thread pool. 更改线程池中的最大线程数时请小心。 While your code might benefit, the changes might have an adverse effect on code libraries you use. 虽然您的代码可能会受益,但所做的更改可能会对您使用的代码库产生不利影响。

If the common language runtime is hosted, for example by Internet Information Services (IIS) or SQL Server, the host can limit or prevent changes to the thread pool size. 如果例如通过Internet信息服务(IIS)或SQL Server托管公共语言运行库,则主机可以限制或阻止对线程池大小的更改。

The CLR or the host (eg IIS) on its own will determine whether to increase or decrease the thread pool size based on the threading patterns of your application so if I were you, I wouldn't bother playing with these numbers. CLR或主机(例如IIS)将根据您的应用程序的线程模式自行决定是增加还是减少线程池的大小,因此,如果您是我,我不会打扰这些数字。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM