简体   繁体   English

我应该在什么时间和上下文中调用ThreadPool.SetMinThreads

[英]At which time and in which context should I call ThreadPool.SetMinThreads

I have an ASP .NET MVC 2 - application, which calls functions of a C#-DLL. 我有一个ASP .NET MVC 2应用程序,该应用程序调用C#-DLL的函数。

The DLL itself is multithreaded. DLL本身是多线程的。 In the worst case it uses up to 200 threads, which do not run very long. 在最坏的情况下,它最多使用200个线程,这些线程不会运行很长时间。

I use asynchronous delegates in order to generate the threads. 我使用异步委托来生成线程。 In order to speed up the initialization of the delegates, I calculate the number of threads I need in advance and give it to the ThreadPool: 为了加快委托的初始化,我预先计算了所需的线程数,并将其提供给ThreadPool:

ThreadPool.SetMinThreads(my_num_threads, ...); 

I just wonder, if I need to do this early enough, such that the ThreadPool has enough time to create the threads? 我只是想知道,是否需要足够早地这样做,以便ThreadPool有足够的时间创建线程? Do I have to consider, when I set the size of the ThreadPool or are the threads available immediately after I call SetMinThreads? 设置ThreadPool的大小是否需要考虑,还是在调用SetMinThreads之后立即可用的线程?

Furthermore, if I set the size outside the DLL in my ASP .NET MVC-application (before I call the DLL), will this setting be available/visible for the DLL? 此外,如果我在ASP .NET MVC应用程序中将DLL的大小设置为超出DLL的大小(在调用DLL之前),则该设置对DLL可用/可见吗?

They share the same application domain so that setting ThreadPool anywhere affects all. 它们共享相同的应用程序域,因此在任何地方设置ThreadPool都会影响全部。 Note that this also impacts on the ASP.NET framework which will use ThreadPool itself for all its own async tasks etc. 请注意,这也会影响ASP.NET框架,该框架会将ThreadPool本身用于所有其自己的异步任务等。

With that in mind, if you knew roughly the minimum number of threads you wanted you could set that on application startup ready for use later on. 考虑到这一点,如果您大致了解所需的最小线程数,则可以在应用程序启动时进行设置,以备后用。

However, 200 threads seems somewhat excessive, to put it into context my Chrome with about 8 tabs open uses about 35 and my SQL Server ~50. 但是,要使用200个线程似乎有些多余,要将其打开,大约8个选项卡打开的Chrome使用35个线程,而使用SQL Server则大约50个线程。 What are you doing that demands so many? 您在做什么呢?

Also realise that eventually you'll reach a limit where performance will degrade as there are so many threads that must be serviced. 还应意识到,最终将达到性能下降的极限,因为必须服务的线程太多。 Microsoft says such on MSDN: 微软在MSDN上这样说:

You can use the SetMinThreads method to increase the minimum number of threads. 您可以使用SetMinThreads方法来增加最小线程数。 However, unnecessarily increasing these values can cause performance problems. 但是,不必要地增加这些值可能会导致性能问题。 If too many tasks start at the same time, all of them might appear to be slow. 如果太多任务同时开始,则它们似乎都变慢了。 In most cases, the thread pool will perform better with its own algorithm for allocating threads. 在大多数情况下,线程池使用自己的分配线程算法会更好地执行。 Reducing the minimum to less than the number of processors can also hurt performance. 将最小值减少到少于处理器数量也会损害性能。

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

相关问题 ThreadPool.SetMinThreads()是否可以将completionPortThreads编号设置为0? - ThreadPool.SetMinThreads() can the completionPortThreads number be set to 0? ThreadPool.SetMinThreads在IIS托管应用程序中不起作用 - ThreadPool.SetMinThreads doesn't work in IIS hosted application 使用准则:C#中的ThreadPool.SetMaxThreads和ThreadPool.SetMinThreads - Guidelines while using: ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads in C# ThreadPool SetMinThreads-设置它的影响 - ThreadPool SetMinThreads - the impact of setting it 在已经在线程池线程上运行的调用上配置等待? - ConfigureAwait on a call which is already running on a threadpool thread? ThreadPool SetMaxThreads和SetMinThreads Magic Number - ThreadPool SetMaxThreads and SetMinThreads Magic Number 我应该在WebRequest调用中注意哪些异常? - Which exceptions should I watch for in a WebRequest call? 如何管理ThreadPool中的哪些项目? - How can I manage which items are in the ThreadPool? 实现一个ThreadPool,它可以在C ++中调用具有多个参数的函数 - Implement a ThreadPool which can call functions with multiple arguments in C++ 我在 UserControl 中有一个按钮,它应该在主窗口中调用一个方法 - I Have a Button in a UserControl which should call a method in the main window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM