多线程连续web响应流,只有一个有效
我正在尝试创建一个执行多个线程的应用程序,并且在该线程中将有一个到网站的连接。我继续阅读该网站(因为它不断发送信息)。
问题是,只有一个线程似乎能够继续从网站读取数据,其他线程似乎无法读取流。当我设置断点时,工作线程会命中该断点,但其他线程不会命中该断点。因此,我查看“线程概述”窗口,看到那里的其他线程以及它的位置“处于睡眠状态,等待或加入”。它也不出现在 try catch 块之一中。
我不知道如何解决这个问题,提前感谢您的帮助。
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要检查两件事:
ServicePointManager
施加的连接限制(请参阅尝试在以下位置运行多个 HTTP 请求并行,但受Windows(注册表)限制)Two things come to mind that you may want to check:
ServicePointManager
(see Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry))