Parallel.ForEachdoes not work with async. He expects Action, but to wait for the asynchronous method he needs to receive Func<Task>.
TPL Dataflow ActionBlock, async. ( ) . parallelism (, , ). :
var block = new ActionBlock<string>(async url =>
{
Uri uri = new Uri(url);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
using (var fileStream = new FileStream(config.M_F_P + filename, FileMode.Create, FileAccess.Write))
{
await content.CopyToAsync(fileStream);
}
}
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 } );
foreach (var url in urls)
{
block.Post(url);
}
block.Complete();
await block.Completion;