0

I wanna try something like download from a collection of image in a minimum of 3 concurrent download but, as soon as one of the 3 finished downloading add a new download from the list or must wait to add a new download until a download is finished from 3 concurrent download. How do I implement something like that?

So far I have tried this, but it seems to download all without waiting to be finished from at least 1 from the 3 concurrent download.

List<string> listOfLink = new List<string>();

await Task.Run(() =>
        Parallel.ForEach(listOfLink, new ParallelOptions { MaxDegreeOfParallelism = 3 }, async (link, state, index) => 
        {
          //Download image httpclient
          //DownloadImageAsync(link);
        }));
4

1 回答 1

2

I'm not entirely sure you need to use Paralell.ForEach here, this answer can explain why better than me re-writing it here: https://stackoverflow.com/a/39796934/5326679

However, to answer your actual question, here's what I'd recommend:

var listOfLink = new List<string>();
var downloadTasks = listOfLink.Select(link => DownloadImageAsync(link));
await Task.WhenAll(downloadTasks);
于 2020-05-10T03:48:08.367 回答