多线程连续web响应流,只有一个有效

发布于 2024-11-18 20:40:18 字数 1082 浏览 1 评论 0原文

我正在尝试创建一个执行多个线程的应用程序,并且在该线程中将有一个到网站的连接。我继续阅读该网站(因为它不断发送信息)。

问题是,只有一个线程似乎能够继续从网站读取数据,其他线程似乎无法读取流。当我设置断点时,工作线程会命中该断点,但其他线程不会命中该断点。因此,我查看“线程概述”窗口,看到那里的其他线程以及它的位置“处于睡眠状态,等待或加入”。它也不出现在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-11-25 20:40:18

您可能需要检查两件事:

  1. 网站可能限制资源的并发请求数量,或者
  2. 您可能达到 ServicePointManager 施加的连接限制(请参阅尝试在以下位置运行多个 HTTP 请求并行,但受Windows(注册表)限制

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文