Multiple DownloadFileAsync files using webclient

Description
Download multiple files using webclient DownloadFileAsync and use a text file to enter the URL to download.

Problem
The approach that I used will not upload files at all. Just running and doing nothing. It fills the list array and then exits without loading a single file. I googled for solutions, but came up with a short one. Then I tried to find a solution in the database here with the same results. Any help is appreciated.

Questions

  • Why does this approach not work?
  • What can I do to improve this and learn from it.

code
DownloadClass.cs

using System; using System.ComponentModel; using System.Collections.Generic; using System.Net; using System.Threading; using System.Windows.Forms; namespace ThreadTest { class DownloadClass { public struct download { public static string URL { get; set; } public static string file { get; set; } public static string[] link; public static int downloadcount; } public static List<string> list = new List<string>(); public static WebClient wc = new WebClient(); public static void Download() { int count = 0; download.URL = list[0]; Uri URI = new Uri(download.URL); UriBuilder uri = new UriBuilder(URI); download.link = uri.Path.ToLower().Split(new char[] { '/' }); count = 0; // Find file foreach (string abs in download.link) { count++; if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt")) { try { download.file = download.link[count]; wc.Proxy = null; wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted); wc.DownloadFileAsync(URI, Application.StartupPath + "\\" + download.file); break; } catch (Exception) { } } } } public static void BeginDownload() { new Thread(Download).Start(); } public static void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { int count = 0; download.downloadcount++; download.URL = list[0]; Uri URI = new Uri(download.URL); UriBuilder uri = new UriBuilder(URI); download.link = uri.Path.ToLower().Split(new char[] { '/' }); count = 0; // Find file foreach (string abs in download.link) { count++; if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt")) { try { download.file = download.link[count]; } catch (Exception) { } } } list.RemoveAt(0); if (list.Count > 0) { wc.DownloadFileAsync(URI, list[download.downloadcount], Application.StartupPath + "\\" + download.file); } else { Console.WriteLine("Downloading is done."); Environment.Exit(0); } } } } 

Program.cs (Main Class)

 using System; using System.IO; using System.Collections.Generic; using System.Windows.Forms; namespace ThreadTest { class Program { static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: {0} <download txtfile>", Environment.GetCommandLineArgs()[0]); Environment.Exit(0); } int counter = 0; string line; string format = string.Format("{0}\\{1}", Application.StartupPath, args[0]); // Read the file line by line. using(StreamReader file = new StreamReader(format)) { while ((line = file.ReadLine())!= null) { // Store urls in a list. DownloadClass.list.Add(line); counter++; } } DownloadClass.BeginDownload(); } } } 
+4
source share
1 answer

Besides poor design, there are many problems that lead to your code not working (or not working normally).

  • You need to make sure that the application is working while it is loading something. Your current application immediately leaves (you need to wait for the download to complete in your main section).
  • An application can download the same file several times, but not download other files (you need to completely block the object when they are used in async = multi-threaded, for example, when accessing static objects). BTW: do not use static objects to avoid this in the first place.
  • Even if 2 is fixed, it can still download the same file several times in the same file name and thus does not work.

Until you know multithreading, I would recommend using synchronous methods to avoid all these problems.

+2
source

Source: https://habr.com/ru/post/1340408/


All Articles