There is a method Get2

  static void Get2(object x) { using (var request = new HttpRequest()) { double n = Convert.ToDouble(x); request.UserAgent = Http.ChromeUserAgent(); request.Cookies = cookies; request.AllowAutoRedirect = false; var res = request.Get("http://ru.stackoverflow.com"); Console.WriteLine(x); } } 

I call it using a thread pool (you need to call the method several times and as quickly as possible)

  num = 1000; numto = 10000; while (num < numto) { for (int i = 0; i < 500; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(Get2), num); num++; } } 

while runs in less than a second, but in the method it reaches the request getter and then Console.WriteLine(x); does not want to go Console.WriteLine(x); - does not output, looked with a sniffer, het request does not pass. But, if you call a method using Thread

  for (int i = 0; i < 500; i++) { Thread myThread = new Thread(new ParameterizedThreadStart(Get2)); myThread.Start(num); num++; } 

That all works, the request passes. The only problem is that this method spends much more time (because the threads are created, and not ready-made and so on). Can you tell me how to solve the problem with the thread pool?

UPD: because it did not find a solution to this problem, instead of xNEt I used the standard HttpWebRequest and everything was decided

    1 answer 1

     var res = request.Get("http://ru.stackoverflow.com"); 

    Probably, there are not enough pairs of using'ov - on the answer and on his Stream.

    The number of simultaneous requests is limited to the value of ServicePointManager.DefaultConnectionLimit .

    • Probably, there are not enough pairs of using'ov - on the answer and on his Stream. can be more ? does this really explain that, if you run it differently, then all the rules? The number of simultaneous requests is limited by the constant ServicePointManager.DefaultConnectionLimit. Can you suggest another way for faster processing of this task? - Lolidze
    • @Lolidze, well, I overdid something with a constant. There is a property that can be set. I do not know how this explains with the flows - probably, really not. But unclosed connections are also considered, so if there is no using'a, the restriction is simply exhausted by several requests, while the others wait .. uh ... probably, until the garbage collector collects other objects, and there it is not at all clear whether it will collect them. bbs.vbstreets.ru/viewtopic.php?f=2&t=43360 - take a look. - Qwertiy