Supose I have this three versiones of one class:
public MainClass
{
public async Task DoSomethingAsync()
{
forech(OtherClass iterator in _otherClasses)
{
iterator.AnAsyncTask().ConfigureAwait(false);
}
}
}
public MainClass
{
public void DoSomething()
{
Parallel.ForEach(_list, async iterator =>
{
await iterator.AnAsyncTask().ConfigureAwait(false);
});
}
}
public MainClass
{
public void DoSomething()
{
Parallel.ForEach(_list, iterator =>
{
iterator.AnAsyncTask().ConfigureAwait(false);
});
}
}
In the first one, I have a ForEach that call an async method but I don’t wait it finish to continue the next item, so in practice, is not the same than run various items in parallel with a parallel ForEach?
In the second one I have a Parallel ForEach, so it runs in parallel various items at the same time.
No matter if I wait or not, this only matters if I would need to do more after the method is finished inside the same iteration.
So my doubt is, which is the difference between to use ForEach or Parallel.Foreach in this case?
Thanks.