- First, you tagged your question with
async-awaitwithout actually using it. There really is no reason anymore to use the old asynchronous paradigms. - To wait asynchronously for all concurrent
asyncoperation to complete you should useTask.WhenAllwhich means that you need to keep all the tasks in some construct (i.e. dictionary) before actually extracting their results. - At the end, when you have all the results in hand you just create the new result dictionary by parsing the
uriinto the file name, and extracting the result out of theasynctasks.
async Task<Dictionary<string, byte[]>> ReturnFileData(IEnumerable<string> urls)
{
var dictionary = urls.ToDictionary(
url => new Uri(url),
url => new WebClient().DownloadDataTaskAsync(url));
await Task.WhenAll(dictionary.Values);
return dictionary.ToDictionary(
pair => Path.GetFileName(pair.Key.LocalPath),
pair => pair.Value.Result);
}
5
solved AWAIT multiple file downloads with DownloadDataAsync