I have a program (.NET 5) that downloads a bunch of files (1k+) simultaneously using WebClient.DownloadFile, which works as expected while running with the debugger, downloading around 99% of the files in both Debug and Release mode; but when running without the debugger it fails to download more than 50% of the files.
All the threads finish before the program ends as they are foreground threads.
The code of the program is:
using System;
using System.IO;
using System.Net;
using System.Threading;
namespace Dumper
{
internal sealed class Program
{
private static void Main(string[] args)
{
Directory.CreateDirectory(args[1]);
foreach (string uri in File.ReadAllLines(args[0]))
{
string filePath = Path.Combine(args[1], uri.Split('/')[^1]);
new Thread((param) =>
{
(string path, string url) = ((string, string))param!;
using WebClient webClient = new();
try
{
webClient.DownloadFile(new Uri(url.Replace("%", "%25")), path);
Console.WriteLine($"{path} has been successfully download.");
}
catch (UriFormatException)
{
throw;
}
catch (Exception e)
{
Console.WriteLine($"{path} failed to download: {e}");
}
}).Start((filePath, uri));
}
}
}
}