Beispiel #1
0
        /// <summary>
        /// Executes a parallel Foreach loop.
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="items">Loop items.</param>
        /// <param name="loopBody">Loop body.</param>
        /// <remarks>The method is used to parallelise for loop by running iterations across
        /// several threads.
        /// Example usage:
        /// <code>
        /// foreach ( Molecule molecule in molecules )
        /// {
        /// System.Diagnostics.Debug.WriteLine( "molecule.Title = " + molecule.Title );
        /// }
        /// </code>
        /// can be replaced by:
        /// <code>
        /// Parallel.ForEach{Molecule}( molecules, delegate( Molecule molecule )
        /// {
        /// System.Diagnostics.Debug.WriteLine( "molecule.Title = " + molecule.Title );
        /// } );
        /// </code>
        /// If <c>Parallel.ThreadCount</c> is exactly <c>1</c>, no threads are spawned.
        /// </remarks>
        public static void ForEach <T>(IEnumerable <T> items, [NotNull, InstantHandle] Action <T> loopBody)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            if (loopBody == null)
            {
                throw new ArgumentNullException("loopBody");
            }
            if (Parallel.threadCount == 1)
            {
                foreach (T item in items)
                {
                    loopBody(item);
                }
            }
            else
            {
                lock (lockObject)
                {
                    ParallelForEach <T> parallel = ParallelForEach <T> .GetInstance(threadCount);

                    parallel.DoForEach(items, loopBody);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Executes a parallel Foreach loop.
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="items">Loop items.</param>
        /// <param name="loopBody">Loop body.</param>
        /// <remarks>The method is used to parallelise for loop by running iterations across
        /// several threads.
        /// Example usage:
        /// <code>
        /// foreach ( Molecule molecule in molecules )
        /// {
        /// System.Diagnostics.Debug.WriteLine( "molecule.Title = " + molecule.Title );
        /// }
        /// </code>
        /// can be replaced by:
        /// <code>
        /// Parallel.ForEach{Molecule}( molecules, delegate( Molecule molecule )
        /// {
        /// System.Diagnostics.Debug.WriteLine( "molecule.Title = " + molecule.Title );
        /// } );
        /// </code>
        /// If <c>Parallel.ThreadCount</c> is exactly <c>1</c>, no threads are spawned.
        /// </remarks>
        public static void ForEach <T>(IEnumerable <T> items, ParallelForEach <T> .ForEachLoopDelegate loopBody)
        {
            if (Parallel.threadCount == 1)
            {
                foreach (T item in items)
                {
                    loopBody(item);
                }
            }
            else
            {
                lock (lockObject) {
                    ParallelForEach <T> parallel = ParallelForEach <T> .GetInstance(threadCount);

                    parallel.DoForEach(items, loopBody);
                }
            }
        }