/// <summary>Loops over each item in the specified <see cref="ICollection{T}" />.</summary> /// <typeparam name="T">The type inside the <see cref="ICollection{T}" />.</typeparam> /// <typeparam name="T1">The additional argument type.</typeparam> /// <param name="Items">The items to loop over.</param> /// <param name="action">The action to preform on each item.</param> /// <param name="TaskCount">The amount of tasks that should process over this <see cref="ICollection{T}" />.</param> /// <param name="Argument">The additional argument needed to process the item.</param> /// <returns>Loops over each item in the specified <see cref="ICollection{T}" />.</returns> public static Task[] ForEach <T, T1>(IList <T> Items, Action <T, T1> action, T1 Argument, Int32 TaskCount) { if (TaskCount < 1) { throw new ArgumentOutOfRangeException(nameof(TaskCount)); } Task[] Out = new Task[Environment.ProcessorCount]; Int32 Step = Items.Count / Out.Length; Int32 Max = Out.Length - 1; Action <Object> InternalAction = (x) => { Parallel.ForEachInternal((ParallelContextCollection <T, T1>)x); }; for (Int32 I = 0; I < Max; I++) { ParallelContextCollection <T, T1> Context = new ParallelContextCollection <T, T1>(I * Step, (I + 1) * Step, Items, action, Argument); Out[I] = Task.Factory.StartNew(InternalAction, Context); } ParallelContextCollection <T, T1> Context1 = new ParallelContextCollection <T, T1>(Max * Step, Items.Count, Items, action, Argument); Out[Max] = Task.Factory.StartNew(InternalAction, Context1); return(Out); }
internal static void ForEachInternal <T>(ParallelContextCollection <T> Context) { Int32 StartIndex = Context._StartIndex; Int32 EndIndex = Context._EndIndex; IList <T> Items = Context._Items; Action <T> Action = Context._Action; for (Int32 I = StartIndex; I < EndIndex; I++) { Action.Invoke(Items[I]); } }