3

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();
        }
    }
Tabriz Atayi
  • 5,880
  • 7
  • 28
  • 33
  • Are you trying to download from several different places? Or are you trying to download a large number of things from the same place? – David Schwartz Jun 30 '12 at 19:52
  • I am trying to download from one IIS server 4 different things – Tabriz Atayi Jun 30 '12 at 19:53
  • 1
    Could you include DownloadFile() body ? – alexm Jun 30 '12 at 19:53
  • 1
    Your code should be able to download any number of files concurrently. Maybe you are hitting some server-side limit? Some file hosts limit the number of active connections from an IP to 2. Try connecting to different file servers. – Simon Ejsing Jun 30 '12 at 19:54
  • Most likely option is its just the server throttling your downloads – Filip Jun 30 '12 at 19:53

1 Answers1

11

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.

Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431