I have a piece of code which is using Parallel.ForEach, probably based on a old version of Rx extensions or the Tasks Parallel Library. I installed a current ve
Here's a simple replacement:
class Parallel
{
public static void ForEach(IEnumerable source, Action body)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (body == null)
{
throw new ArgumentNullException("body");
}
var items = new List(source);
var countdown = new CountdownEvent(items.Count);
WaitCallback callback = state =>
{
try
{
body((T)state);
}
finally
{
countdown.Signal();
}
};
foreach (var item in items)
{
ThreadPool.QueueUserWorkItem(callback, item);
}
countdown.Wait();
}
}