1
I am developing a class for downloading C files#: Filedownloader. The problem is occurring in using it. I am trying to make simultaneous downloads, it is working normal when it is running outside the Parallel
but when it spins inside it using (var response = await request.GetResponseAsync()) // line 205 da classe DownloadFile
is waiting forever ignoring the TimeOut
and does not download.
private async void DoDownload(string[] segments)
{
//Whether to stop downloading
stopdownload = false;
cts = new CancellationTokenSource();
//Multi-threaded setup
ParallelOptions parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = 2,
CancellationToken = cts.Token
};
//Start Downloading
OnDownloadStart?.Invoke(this, EventArgs.Empty);
DownloadStatus = DownloadStatus.Downloading;
var downloader1 = new Downloader.DownloadFile()
{
Url = @"https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2021-03-13-12-31/ffmpeg-n4.3.2-160-gfbb9368226-win64-gpl-4.3.zip",
FilePath = Path.Combine(OutFolder.FullName, $"part_10.zip")
};
await downloader1.StartAsync();
try
{
Parallel.ForEach(segments, parallelOptions, async (info, loopstate, index) =>
{
if (stopdownload)
loopstate.Stop();
var downloader2 = new Downloader.DownloadFile()
{
Url = @"https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2021-03-13-12-31/ffmpeg-n4.3.2-160-gfbb9368226-win64-gpl-4.3.zip",
FilePath = Path.Combine(OutFolder.FullName, $"part_{index}.zip")
};
await downloader2.StartAsync();
});
}
catch (OperationCanceledException)
{
;
}
}
The downloader1
drops normally while the downloader2
no. I fixed the url to one that I was sure was working. But why is this happening?
Thanks for the reply, I removed the
async
of the code and again made the downloads normally only that generated another problem. Both in its implementation and in the withdrawal of theasync
both ways make as many downloads as possible, but I need to have control over how many files I’m downloading at a time. For example some cases I will not be able to pass 2 and others 4 simultaneous downloads.– Lucas Pedro