使用WebClient多个任务超时

时间:2012-11-23 10:25:41

标签: c# download task webclient

我使用这个代码下载文件:

public delegate void ReportProgerssDelegate(int progerss);

public class TestFileDownloader
{
    public ReportProgerssDelegate ReportProgerss;

    public void startHTTPDownload(string urlToReadFileFrom, string filePathToWriteFileTo)
    {
        Uri _url = new Uri(urlToReadFileFrom);

        System.Net.HttpWebRequest _request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(_url);
        System.Net.HttpWebResponse _response = (System.Net.HttpWebResponse)_request.GetResponse();

        Int64 _downloadedFileSize = _response.ContentLength;
        Int64 _totalNumberOfReadedBytes = 0;

        using (System.Net.WebClient _webClient = new System.Net.WebClient())
        {
            using (System.IO.Stream _streamRemote = _webClient.OpenRead(new Uri(urlToReadFileFrom)))
            {
                using (Stream _streamLocal = new FileStream(filePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    int _numberOfReadedBytes = 0;
                    byte[] _byteBuffer = new byte[1024];

                    while ((_numberOfReadedBytes = _streamRemote.Read(_byteBuffer, 0, _byteBuffer.Length)) > 0)
                    {
                        _streamLocal.Write(_byteBuffer, 0, _numberOfReadedBytes);
                        _totalNumberOfReadedBytes += _numberOfReadedBytes;

                        reportProgress(_totalNumberOfReadedBytes, _downloadedFileSize);
                    }
                    _streamLocal.Close();
                }
                _streamRemote.Close();
            }
        }
        reportProgress(100, 100);
    }

    private void reportProgress(long totalNumberOfReadedBytes, long downloadedFileSize)
    {
        double _index = (double)(totalNumberOfReadedBytes);
        double _total = (double)downloadedFileSize;
        double _progress = (_index / _total);
        int _progressPercentage = (int)(_progress * 100);

        if (ReportProgerss != null)
            ReportProgerss(_progressPercentage);
    }
}

当我将它作为单个任务启动时,它的工作非常完美。

        TestFileDownloader _fd1 = new TestFileDownloader();
        _fd1.ReportProgerss = reportProgress;

        Task _task = new Task(
          () => _fd1.startHTTPDownload("http://", 
                                      @"d:\..."));
        _task.Start();

当我尝试使用它时:

            TestFileDownloader _fd1 = new TestFileDownloader();
            _fd1.ReportProgerss = reportProgress;
            TestFileDownloader _fd2 = new TestFileDownloader();
            _fd2.ReportProgerss = reportProgress;
            TestFileDownloader _fd3 = new TestFileDownloader();
            _fd3.ReportProgerss = reportProgress;

            Task _task = new Task(
              () => _fd1.startHTTPDownload("http://", 
                                          @"d:\..."));
            _task.Start();

            Task _task2 = new Task(
              () => _fd2.startHTTPDownload("http://",
                                          @"d:\..."));
            _task2.Start();

            Task _task3 = new Task(
              () => _fd3.startHTTPDownload("http://",
                                          @"d:\..."));
            _task3.Start();

我迟到了。

有什么问题?

1 个答案:

答案 0 :(得分:0)

这篇文章帮助了我:

Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)

通过设置ServicePointManager.DefaultConnectionLimit

解决了我的问题