I am stuck in a situation where I have to download multiple files from a list of urls. I found Parallel.ForEach
as a solution and it really works faster than a simple foreach. But this solution is not waiting till all downloads are done.
List<string> list = new List<string>() {"url1","url2","url3",... };
public async void downloadSimpleFile(string fileUrl) {
//download file logic
}
await Task.Factory.StartNew(() => Parallel.ForEach(list,
new ParallelOptions { MaxDegreeOfParallelism = 10 }, downloadSimpleFile));
I was also tying:
public async Task<bool> downloadSimpleFile(string fileUrl) {
//download file logic
return true;
}
Now how can I use downloadSimpleFile
in Parallel.ForEach
, because up mentioned code is not working anymore?
I was reading some posts about async-await in Parallel
like this or this but I'm not getting the idea. Any ideas are apreciated.