【问题标题】:DownloadFileTaskAsync downloads only 2 files at a timeDownloadFileTaskAsync 一次只下载 2 个文件
【发布时间】:2016-05-18 13:00:01
【问题描述】:

我已经实现了下面线程中的代码,但是我看到所有文件都开始下载,但一次只下载 2 个文件

Asynchronously and parallelly downloading files

private static async Task<Tuple<string, string, Exception>> DownloadFileTaskAsync(string remotePath, string localPath = null, int timeOut = 3000)
        {
            try
            {

                localPath = localPath + remotePath.Split('/')[5].ToString();

                using (var client = new WebClient())
                {
                    client.Credentials = new NetworkCredential("user", "password");

                    using (var timer = new Timer(timerCallback, client, timeOut, Timeout.Infinite))
                    {
                        await client.DownloadFileTaskAsync(remotePath, localPath);
                    }
                    Debug.WriteLine(string.Format("DownloadFileTaskAsync (downloaded): {0}", remotePath));
                    return new Tuple<string, string, Exception>(remotePath, localPath, null);
                }
            }
            catch (Exception ex)
            {
                return new Tuple<string, string, Exception>(remotePath, null, ex);
            }
        }


        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            IEnumerable<string> enumerable = new string[] {"https://somepath.com/file1.zip" };
            var results = new List<Tuple<string, string, Exception>>();
            await enumerable.ForEachAsync(s => DownloadFileTaskAsync(s, "C:\\Download\\", 10000), (url, t) => results.Add(t));

        }

    }

    public static class Extensions
    {
        public static Task ForEachAsync<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Task<TResult>> taskSelector, Action<TSource, TResult> resultProcessor)
        {
            var oneAtATime = new SemaphoreSlim(5, 10);
            return Task.WhenAll(
                from item in source
                select ProcessAsync(item, taskSelector, resultProcessor, oneAtATime));
        }

        private static async Task ProcessAsync<TSource, TResult>(TSource item, Func<TSource, Task<TResult>> taskSelector, Action<TSource, TResult> resultProcessor, SemaphoreSlim oneAtATime)
        {
            TResult result = await taskSelector(item);
            await oneAtATime.WaitAsync();
            try
            {
                resultProcessor(item, result);
            }
            finally
            {
                oneAtATime.Release();
            }
        }
    }

【问题讨论】:

标签: .net


【解决方案1】:

这是因为 2 是 ServicePointManager 允许的默认并发连接数。

解决方法很简单:

System.Net.ServicePointManager.DefaultConnectionLimit = 10; // Whatever you want it to be

【讨论】:

  • 非常感谢基里尔!如果增加数字,它会造成任何性能损失还是有推荐的数字
  • @SSK,这取决于场景,但快速搜索显示号码12 * &lt;num CPU cores&gt; 不断出现。这是否会产生任何问题在很大程度上取决于您的 target 主机准备从您的 IP 接受的并发连接数。您可以在他们的 API 规范中或通过反复试验找到这一点。
猜你喜欢
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 2019-01-30
相关资源
最近更新 更多