0

Let's say I have the following code :

ThreadPool.SetMinThreads(100, 100); 
for (int i = 0; i < 100; i++)
{
  var request = WebRequest.Create(url);
  request.BeginGetResponse(ar =>
 {
  //inside AsynchCallBack method for request.BeginGetResponse()
  var response = (HttpWebResponse)request.EndGetResponse(ar);

  string html;
  using (var receiveStream = response.GetResponseStream())
  using (var readStream = new StreamReader(receiveStream
                       , Encoding.UTF8))
  {
   html = readStream.ReadToEnd();
  }

            Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

 }, null
 );
}

I'm expecting to see quite a lot of threads when writing to the console the ManagedThreadId - of course I am wrong :) . I generally see only 2 different thread Ids and once in a while 3 thread Ids.

Why this behavior ? What am I missing?

skaffman
  • 398,947
  • 96
  • 818
  • 769
sirrocco
  • 7,975
  • 4
  • 59
  • 81

1 Answers1

3

I think you are hitting the connection limit.

jsight
  • 27,819
  • 25
  • 107
  • 140