|
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Text;
using System;
namespace Rob.Utils
{
public static class ParallelWrapper
{
#region Public Methods
public static void forEach<T>(IEnumerable<T> collection, Action<T> action)
{
ConcurrentQueue<Exception> exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach<T>(collection, element =>
{
try
{
action(element);
}
catch (Exception ex)
{
exceptions.Enqueue(ex);
}
});
if (exceptions.Count > 0)
throw new AggregateException(exceptions);
}
public static void invoke(params Action[] actions)
{
ConcurrentQueue<Exception> exceptions = new ConcurrentQueue<Exception>();
Parallel.Invoke(actions.Select(action => new Action(() =>
{
try
{
action();
}
catch (Exception ex)
{
exceptions.Enqueue(ex);
}
})).ToArray());
if (exceptions.Count > 0)
throw new AggregateException(exceptions);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.