c# Threadpool - limit number of threads

后端 未结 6 1786
北恋
北恋 2020-12-24 02:46

I am developing a console app.

I want to use a Threadpool to perform web downloads. Here is some fake code.

 for (int loop=0; loop< 100; loop++)
         


        
6条回答
  •  [愿得一人]
    2020-12-24 03:15

    Look at the parameters of ThreadPool.SetMaxThreads. The first parameter is the amount of worker threads and the second parameter is the amount of async threads, which is which one you're talking about.

    Further down the documentation, it says:

    You cannot set the number of worker threads or the number of I/O completion threads to a number smaller than the number of processors in the computer.

    It sounds like you're trying to use the ThreadPool for something it's not intended to be used for. If you want to limit the amount of downloads create a class that manages this for you, because the ThreadPool isn't necessarily the complete solution to your problem.

    I'd suggest a class that starts two threads in the ThreadPool and waits for the callback. When it receives a callback for the completion of one of the threads queue a new one.

提交回复
热议问题