public void Foreach(ForeachDelegate callback) { for (var x = 0; x < _width; x++) { for (var y = 0; y < _height; y++) { callback.Invoke(x, y); } } }
private static void ForEach(this System.Collections.IEnumerable coll, ForeachDelegate del) { if (coll != null) { foreach (var item in coll) { del(item as string); } } }
private static void ForeachTask <T>(T[] data, int length, ForeachDelegate <T> loopbody, Async parent) { for (int i = 0; i < length; i++) { loopbody(data[i]); } if (parent != null) { parent.WaitFor(); } }
public void ForeachFile(ForeachDelegate callback) { lock (lockObject) { Touch(); foreach (FileModel model in files.Values) { if (!callback(model)) { break; } } } }
/// <summary> /// A parallel foreach. /// </summary> /// <remarks> /// Parallel.Foreach is similar to a standard foreach. Loopbody, which takes an element of type T, /// is called once for each element in the collection. Hence loopbody contains the code you would normally /// put inside the foreach loop.<p/> /// GrainSize specifies a reasonable number of iterations in each task. If the number of iterations is more than grainSize, data /// is split in two and handled separately. /// Adjusting the grain size can lead to better performance, but the optimal grain /// size depends on the problem at hand. Increasing the grain size will decrease the amount of tasks, /// and thus decrease the overhead of setting up tasks. But a small amount of tasks might introduce some load balancing problems, /// if no tasks are available for free processors. Decreasing the grain size will increase the amount of tasks and thereby ensure /// better load balancing, but on the other hand it will also increase the overhead of setting up tasks.<p/> /// Parallel.Foreach catches any uncaught exception raised inside the tasks. Once the exception is caught Parallel.Foreach cancels all /// running tasks that it has started and throws a Jibu.CancelException, containing the original exception. Jibu cancels all /// tasks but it doesn't just brutally stop them - the user defined code must detect and handle the cancellation. For more information on cancellation see Jibu.Task.Cancel and Jibu.CancelException /// </remarks> /// <exception cref="Jibu.CancelException">If one of the Tasks is cancelled, Parallel.Foreach cancels the remaining Tasks /// and throws a Jibu.CancelException.</exception> /// <exception cref="System.ArgumentOutOfRangeException">Is thrown if the grain size is less than 1.</exception> /// <typeparam name="T">Type of collection</typeparam> /// <param name="collection">An IEnumerable collection of type T</param> /// <param name="grainSize">A reasonable number of iterations in each task</param> /// <param name="loopbody">A delegate containing the work. Loopbody is executed once for each iteration.</param> // <example> // - Parallel ForEach Example - // <code><include ForEachExample/ForEachExample.cs></code> // </example> public static void Foreach <T>(IEnumerable <T> collection, int grainSize, ForeachDelegate <T> loopbody) { if (grainSize < 1) { throw new ArgumentOutOfRangeException("Grain size cannot be less than 1."); } new DelegateAsync( delegate { IEnumerator <T> ie = collection.GetEnumerator(); Async parent = null; while (ie.MoveNext()) { T[] data = new T[grainSize]; int i = 0; do { data[i] = ie.Current; i++; }while (i < grainSize && ie.MoveNext()); if (i == grainSize) { Async w = parent; parent = new DelegateAsync(delegate { ForeachTask(data, i, loopbody, w); }).Start(); } else { ForeachTask(data, i, loopbody, parent); return; } } if (parent != null) { parent.WaitFor(); } }).WaitFor(); }
private static void ForEach(this System.Collections.Generic.IEnumerable <string> coll, ForeachDelegate del) { foreach (var item in coll) { del(item); } }
public void ForeachFile(ForeachDelegate callback) { lock (lockObject) { Touch(); foreach (FileModel model in files.Values) if (!callback(model)) break; } }
/// <summary> /// A parallel foreach. /// </summary> /// <remarks> /// Parallel.Foreach is similar to a standard foreach. Loopbody, which takes an element of type T, /// is called once for each element in the collection. Hence loopbody contains the code you would normally /// put inside the foreach body.<p/> /// Parallel.Foreach catches any uncaught exception raised inside the tasks. Once the exception is caught Parallel.Foreach cancels all /// running tasks that it has started and throws a Jibu.CancelException, containing the original exception. Jibu cancels all /// tasks but it doesn't just brutally stop them - the user defined code must detect and handle the cancellation. For more information on cancellation see Jibu.Task.Cancel and Jibu.CancelException.<p/> /// In this version of Foreach no grain size is specified and each iteration is processed in its own task. /// </remarks> /// <exception cref="Jibu.CancelException">If one of the Tasks is cancelled, Parallel.Foreach cancels the remaining Tasks /// and throws a Jibu.CancelException.</exception> /// <typeparam name="T">Type of collection</typeparam> /// <param name="collection">An IEnumerable collection of type T</param> /// <param name="loopbody">A delegate containing the work. Loopbody is executed once for each iteration.</param> // <example> // - Parallel ForEach Example - // <code><include ForEachExample/ForEachExample.cs></code> // </example> public static void Foreach <T>(IEnumerable <T> collection, ForeachDelegate <T> loopbody) { Foreach(collection, 1, loopbody); }