Télécharger plusieurs fichiers en parallèle à l'aide de c#

Je veux télécharger des fichiers en parallèle à l'aide de C#. Pour cela, j'ai écrit ce code qui fonctionne parfaitement, mais le problème est que l'INTERFACE utilisateur est la congélation.

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);
}
}
}
Vous devriez être en utilisant asynchrone, pas en parallèle. Vous n'avez pas liée au calcul de travail.
Qu'espérez-vous gagner? Ce ne sera pas liée à l'UC.
David Heffernan: je suis à l'exception du fichier téléchargé, parallèlement, sans le figer l'INTERFACE utilisateur
Ce que vous voulez faire avec les fichiers téléchargés?
enregistrer ces fichiers sur le système de fichiers local

OriginalL'auteur santosh singh | 2014-03-30