1

I'm trying to create an application that executes multiple threads and in that thread there will be a connection to a website. I keep reading the website (as it keeps sending information).

The problem is that only one thread seems to be able to keep reading from the website, the other threads looks like there are not able to read the stream.When I set an breakpoint the working threads hit it but the others don't. So I look in Threads overview window and see the other thread there and as location it has 'In a sleep, wait or join'. It also doesn't come in one of the try catch blocks.

I don't know how to solve this, thanks in advance for your help.

    HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(domain);
    request.Credentials = new NetworkCredential(Username, Password);

    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    using (response)
    {
      Stream resStream = response.GetResponseStream();
      using (resStream)
      {
        int count;
        do
        {
          count = resStream.Read(buf, 0, buf.Length);
          // make sure we read some data);
          if (count != 0)
          {
            string tempString = Encoding.ASCII.GetString(buf, 0, count);
            QueueResponse(tempString);
          }
          catch (Exception e)
          {
            Console.WriteLine("Exception occured");
          }
          Thread.Sleep(1000);
        } while (count > 0); 
      }
    }
Grambot
  • 4,370
  • 5
  • 28
  • 43
BvdVen
  • 2,921
  • 23
  • 33
  • My intiuition is that your environment is unable to support multiple threads at a time. Although you've multi-threaded your application only one thread is able to run at a time until the scheduler allows the other thread(s) a turn at the processor/resource they are waiting on. If you run/pause run/pause the application numerous times is it always the same thread running and the others waiting? – Grambot Jul 07 '11 at 18:01
  • You may want a second take at the formatting. – H H Jul 07 '11 at 18:07
  • OK, so how does this code tie in, can QueueResponse() block, why is the Sleep() there? – H H Jul 07 '11 at 18:09
  • The sleep is to allow other threads to do their work, the queueresponse is just an method that adds the string to a list – BvdVen Jul 07 '11 at 19:43

1 Answers1

1

Two things come to mind that you may want to check:

  1. The website might be limiting the number of concurrent requests for the resource, or
  2. You might be reaching a connection limit imposed by the ServicePointManager (see Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry))
Community
  • 1
  • 1
Michael Petito
  • 12,891
  • 4
  • 40
  • 54