【问题标题】:Download multiple files in parallel using c#使用c#并行下载多个文件
【发布时间】:2019-01-30 22:22:05
【问题描述】:

我想使用 C# 并行下载文件。为此,我编写了这段代码,它运行良好,但问题是 UI 卡住了。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace FileDownloader
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static int count = 1;
        private static string f= "lecture";
        private string URL = "www.someexample.com";

        public MainWindow()
        {
            InitializeComponent();
        }

        public static string GetDirectoryListingRegexForUrl(string url)
        {
            if (url.Equals(URL))
            {
                return "<a href=\".*\">(?<name>.*)</a>";
            }
            throw new NotSupportedException();
        }

        public  void DownloadP(string[] urls)
        {
            Parallel.ForEach(urls.ToList(), new ParallelOptions { MaxDegreeOfParallelism = 10 }, DownloadFile);
        }

        private void DownloadFile(string url)
        {
           using(WebClient client=new WebClient())
           {
               if (url.EndsWith(".pdf"))
               {
                   int nextIndex = Interlocked.Increment(ref count);

                   client.DownloadFile(url, f + nextIndex + ".pdf");
                   this.Dispatcher.Invoke(() => {
                       listbox.Items.Add(url);
                   });
               }
           }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DownloadP(listofFiles);
        }
    }
}

【问题讨论】:

  • 您应该使用异步,而不是并行。您没有任何计算密集型工作。
  • 您希望获得什么?这将不受 CPU 限制。
  • @SLaks & David Heffernan:我在不冻结 UI 的情况下并行下载文件除外
  • 你想对下载的文件做什么?
  • @HamletHakobyan:只想把那些文件保存在本地文件系统上

标签: c# task-parallel-library


【解决方案1】:

您可以将async/await 与新的WebClient 方法DownloadFileTaskAsync 结合使用。

private async Task DownloadFile(string url)
{
    if (!url.EndsWith(".pdf"))
    {
        return;
    }

    using (var client = new WebClient())
    {
        int nextIndex = Interlocked.Increment(ref count);

        await client.DownloadFileTaskAsync(url, "lecture" + nextIndex + ".pdf");
        listBox.Items.Add(url);

    }
}

private async void Button_OnClick(object sender, RoutedEventArgs e)
{
    button.IsEnabled = false;
    await DownloadFiles(urlList);
    button.IsEnabled = true;
}

private async Task DownloadFiles(IEnumerable<string> urlList)
{
    foreach (var url in urlList)
    {
        await DownloadFile(url);
    }
}

【讨论】:

  • 你为什么在这里使用Interlocked.Increment?此代码中的所有内容都在同一个 UI 线程上运行(DownloadFileTaskAsync 内部除外)。
  • @HamletHakobyan,嗯好的 :)
  • 您的代码不会冻结 UI 线程,但也不会因为您在循环中等待而并行运行。这有点误导,与OP的主题标题不一致。
【解决方案2】:

用这个替换你的 DownloadP 函数:

public async Task DownloadP(string[] urls)
{
  await Task.Factory.StartNew(() => Parallel.ForEach(urls.ToList(), new ParallelOptions { MaxDegreeOfParallelism = 10 }, DownloadFile));
}

【讨论】:

  • mmm async void ))
【解决方案3】:

像这样使用 client.DownloadFileAsync 而不是使用 client.DownloadFile

var webClient=new WebClient();
webClient.DownloadFileCompleted += webClient_DownloadFileCompleted;
webClient.DownloadFileAsync("Your url","file_name");

事件

    private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Download Completed.");
    }

【讨论】:

  • +1 好建议 - 请确保使用原始帖子中的通知代码更新您的答案 - MessageBox.Show 不是设计用于并行执行许多操作的代码的最佳选择。
  • 我将如何在 Parallel.ForEach 循环中处理这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多