c# Threadpool - limit number of threads

后端 未结 6 1803
北恋
北恋 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:18

    Description

    You can do this using the ThreadPool.SetMaxThreads method.

    But there are some problems using the ThreadPool for WebRequest. Read, for example, this (Bug in ThreadPool or HttpWebRequest?)

    Sample

    ThreadPool.SetMaxThreads(2,2);
    

    Edit

    Personally i would use AsParallel from Linq, for this.

    More Information

    • MSDN - ThreadPool.SetMaxThreads Method
    • MSDN - ParallelEnumerable.AsParallel Method

提交回复
热议问题