多线程连续webresponse流,只有一个工作

本文关键字:有一个 工作 连续 webresponse 多线程 | 更新日期: 2023-09-27 18:01:33

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

问题是,似乎只有一个线程能够继续从网站读取,其他线程似乎无法读取流。当我设置一个断点时,工作线程击中它,但其他线程没有。所以我看在线程概述窗口,看到那里的其他线程和位置,它有'在睡眠中,等待或加入'。它也不会出现在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); 
      }
    }

多线程连续webresponse流,只有一个工作

您可能需要检查两件事:

  1. 网站可能限制了资源的并发请求数,或者
  2. 您可能达到ServicePointManager施加的连接限制(参见尝试并行运行多个HTTP请求,但受到Windows(注册表)的限制)
相关文章: