Multi-threaded continuous webresponse stream, only one works
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 开发者_Go百科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);
}
}
Two things come to mind that you may want to check:
- The website might be limiting the number of concurrent requests for the resource, or
- 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))
精彩评论