Quelles sont les différences entre l'utilisation de Parallel.ForEach ou de Task.Run () pour démarrer un ensemble de tâches de manière asynchrone?
Version 1:
List<string> strings = new List<string> { "s1", "s2", "s3" };
Parallel.ForEach(strings, s =>
{
DoSomething(s);
});
Version 2:
List<string> strings = new List<string> { "s1", "s2", "s3" };
List<Task> Tasks = new List<Task>();
foreach (var s in strings)
{
Tasks.Add(Task.Run(() => DoSomething(s)));
}
await Task.WhenAll(Tasks);
Task.WaitAll
place deTask.WhenAll
.