C# 使用c语言并行下载多个文件#

C# 使用c语言并行下载多个文件#,c#,task-parallel-library,C#,Task Parallel Library,我想用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.Tas

我想用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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Text.RegularExpressions;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Windows.Threading;
命名空间文件下载程序
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
私有静态整数计数=1;
私有静态字符串f=“讲座”;
私有字符串URL=“www.someexample.com”;
公共主窗口()
{
初始化组件();
}
公共静态字符串GetDirectoryListingRegexForUrl(字符串url)
{
if(url.Equals(url))
{
返回“”;
}
抛出新的NotSupportedException();
}
public void DownloadP(字符串[]URL)
{
Parallel.ForEach(url.ToList(),新的ParallelOptions{MaxDegreeOfParallelism=10},下载文件);
}
私有void下载文件(字符串url)
{
使用(WebClient=newWebClient())
{
如果(url.EndsWith(“.pdf”))
{
int nextIndex=联锁增量(参考计数);
client.DownloadFile(url,f+nextIndex+“.pdf”);
this.Dispatcher.Invoke(()=>{
listbox.Items.Add(url);
});
}
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
下载文件;
}
}
}

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

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.");
    }

您可以将
async/await
与新的
WebClient
方法
下载文件任务async
结合使用

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);
    }
}
私有异步任务下载文件(字符串url)
{
如果(!url.EndsWith(“.pdf”))
{
返回;
}
使用(var client=new WebClient())
{
int nextIndex=联锁增量(参考计数);
等待client.DownloadFileTaskAsync(url,“讲座”+nextIndex+“.pdf”);
listBox.Items.Add(url);
}
}
私有异步无效按钮\u OnClick(对象发送方,路由目标)
{
button.IsEnabled=false;
等待下载文件(URL列表);
button.IsEnabled=true;
}
专用异步任务下载文件(IEnumerable urlList)
{
foreach(url列表中的变量url)
{
等待下载文件(url);
}
}

使用以下命令重新启动下载功能:

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

您应该使用异步,而不是并行。您没有任何计算机限制的工作。您希望获得什么?这不会受到CPU的限制。@SLaks&David Heffernan:我例外,并行下载的文件不会冻结UIS,您想对下载的文件做什么?@HamletHakobyan:只想将这些文件保存在本地文件系统上+1个好建议-请确保使用原始帖子中的通知代码更新您的答案-
MessageBox.Show
不是设计用于并行执行许多操作的代码的最佳选择。我将如何并行处理此问题。ForEach loop为什么要在此处使用
Interlocked.Increment
?这段代码中的所有内容都在同一个UI线程上运行(除了
DownloadFileTaskAsync
internals)。@HamletHakobyan,啊,好的:)您的代码不会冻结UI线程,但也不会在循环中等待时并行运行。这有点误导,与OP的主题标题不一致。mmm async void)