最大线程数

3

我有一个用户控件,其中包含以下方法

    public void DownloadFileAsync()
    {
        ThreadStart thread = DownloadFile;
        Thread downloadThread = new Thread(thread);

        downloadThread.Start();
    }

在表单中,我有4个用户控件,如此设置。但是当我为每个控件调用用户控件DownloadFileAsync()时,只有其中两个开始下载。完成其中一个后,下一个开始下载。
问题是什么,我如何同时下载每个文件?
感谢您的关注。
    public void DownloadFile()
    {
        int byteRecieved = 0;
        byte[] bytes = new byte[_bufferSize];

        try
        {
            _webRequestDownloadFile = (HttpWebRequest)WebRequest.Create(_file.AddressURL);
            _webRequestDownloadFile.AddRange((int)_totalRecievedBytes);
            _webResponseDownloadFile = (HttpWebResponse)_webRequestDownloadFile.GetResponse();
            _fileSize = _webResponseDownloadFile.ContentLength;
            _streamFile = _webResponseDownloadFile.GetResponseStream();

            _streamLocalFile = new FileStream(_file.LocalDirectory, _totalRecievedBytes > 0 ? FileMode.Append : FileMode.Create);

            MessageBox.Show("Salam");
            _status = DownloadStatus.Inprogress;
            while ((byteRecieved = _streamFile.Read(bytes, 0, _bufferSize)) > 0 && _status == DownloadStatus.Inprogress)
            {

                _streamLocalFile.Write(bytes, 0, byteRecieved);

                _totalRecievedBytes += byteRecieved;

                if (_totalRecievedBytes >= _fileSize)
                {
                    argsCompleted.Status = DownloadStatus.Completed;
                    if (_fileDownloadCompleted != null)
                        _fileDownloadCompleted(_file, argsCompleted);
                    break;
                }

                argsProgress.UpdateEventArgument(DownloadStatus.Inprogress, _totalRecievedBytes);

                if (_fileDownloadProgress != null)
                    _fileDownloadProgress(_file, argsProgress);



            }
        }
        catch (Exception ex)
        {
            LogOperations.Log(ex.Message);
        }
        finally
        {
            _streamFile.Close();
            _streamFile.Close();
            _streamLocalFile.Close();
            _webResponseDownloadFile.Close();
        }
    }

你是在尝试从几个不同的地方下载吗?还是在尝试从同一个地方下载大量的东西? - David Schwartz
最有可能的选项是服务器限制了您的下载速度。 - Filip
我正在尝试从一个IIS服务器下载4个不同的东西。 - Tabriz Atayi
1
你能包含DownloadFile()的主体吗? - alexm
1
你的代码应该能够同时下载任意数量的文件。也许你遇到了一些服务器端的限制?有些文件主机将一个IP的活动连接数限制为2。尝试连接到不同的文件服务器。 - Simon Ejsing
1个回答

11

HTTP 长期以来每个 Web Origin 的连接限制为2个,这样人们就不会同时开始太多下载。虽然这个限制已经被解除,但许多实现,包括 HttpWebRequest 仍然实现它。

draft-ietf-httpbis-p1-messaging 中可以看到:

客户端(包括代理)应限制维护到给定服务器(包括代理)的并发连接数。

HTTP 的早期版本将特定数量的连接作为上限,但发现这对许多应用程序来说是不切实际的。因此,该规范没有强制规定特定的最大连接数,而是鼓励客户端在打开多个连接时保持谨慎。

您可以通过设置 ConnectionLimit Property 更改连接限制,如下所示:

HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
httpWebRequest.ServicePoint.ConnectionLimit = 10;

不要将限制设置得太高,以免过载服务器。
此外,您应该考虑使用WebClient Class和其提供的异步方法(如DownloadDataAsync Method)代替使用线程。请参见How can I programmatically remove the 2 connection limit in WebClient以了解如何在此处更改连接限制。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接