简体   繁体   中英

Maximum Thread Number

I have user control which has method as this

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

        downloadThread.Start();
    }

In the form I have 4 user control as this. But when I call in the user controls DownloadFileAsync() for each control only two of them begin to download. After finishing one of them the next begins to downloads.

What is the problem and how I can download simultanesly each download?

Thank you for your attention.

    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();
        }
    }

HTTP had a limit of 2 connections per Web Origin for a very long time, so people wouldn't start too many downloads in parallel. This limit has been lifted, but many implementations including HttpWebRequest still implement it.

From draft-ietf-httpbis-p1-messaging :

Clients (including proxies) SHOULD limit the number of simultaneous connections that they maintain to a given server (including proxies).

Previous revisions of HTTP gave a specific number of connections as a ceiling, but this was found to be impractical for many applications. As a result, this specification does not mandate a particular maximum number of connections, but instead encourages clients to be conservative when opening multiple connections.

You can change the connection limit by setting the ConnectionLimit Property as follows:

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

Don't set the limit too high, so you don't overload the server.

Also, instead of using threads, you should consider using the WebClient Class and the asynchronous methods it provides (such as the DownloadDataAsync Method ). See How can I programmatically remove the 2 connection limit in WebClient for how to change the connection limit here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM