コード例 #1
0
    public static void Explain(this ParallelForEach runnable, TextWriter writer)
    {
        writer.WriteLine(@"
- Similar to `Parallel.For` but this time foreach
- Why is this behaving differently in comparison to `Parallel.For`?
");
    }
コード例 #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, [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);
                }
            }
        }
コード例 #3
0
        private static ActivityInfo[] GetParallelForEachChildren <T>(ParallelForEach <T> activity)
        {
            var parallelForEach = (dynamic)activity;

            var children = new List <ActivityInfo>();

            if (parallelForEach.Body != null && parallelForEach.Body.Handler != null)
            {
                children.Add(new ActivityInfo(parallelForEach.Body.Handler, activity, "Body"));
            }

            return(children.ToArray());
        }
コード例 #4
0
        static void Main(string[] args)
        {
            DataContractSerialization.Start(null);
            BinarySerialization.Start(null);
            XmlSerialization.Start(null);

            Assemblies.Start(null);
            Reflect.Start(null);

            TLS.Start(null);

            Linq.Start(null);

            ParallelInvoke.Start(null);
            ParallelFor.Start(null);
            ParallelForEach.Start(null);
        }
コード例 #5
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);
                }
            }
        }
コード例 #6
0
 /// <summary>
 /// Get instance of the ParallelFor class for singleton pattern and
 /// update the number of threads if appropriate.
 /// </summary>
 /// <param name="threadCount">The thread count.</param>
 /// <returns></returns>
 public static ParallelForEach <T> GetInstance(int threadCount)
 {
     if (instance == null)
     {
         instance             = new ParallelForEach <T>();
         instance.threadCount = threadCount;
         instance.Initialize();
     }
     else
     {
         // Ensure we have the correct number of threads.
         if (instance.workerThreads.Count != threadCount)
         {
             instance.Terminate();
             instance.threadCount = threadCount;
             instance.Initialize();
         }
     }
     return(instance);
 }
コード例 #7
0
ファイル: Program.cs プロジェクト: Ahura007/loop
        static void Main(string[] args)
        {
            var           someData = Enumerable.Range(0, 1000000000).ToList();
            StringBuilder sb       = new StringBuilder();

            sb.Append("for Property             " + For.Property(someData) + Environment.NewLine);
            sb.Append("for Variable             " + For.Variable(someData.Count) + Environment.NewLine);

            sb.Append("parallel For Property    " + ParallelFor.Property(someData) + Environment.NewLine);
            sb.Append("parallel For Variable    " + ParallelFor.Variable(someData.Count) + Environment.NewLine);

            sb.Append("while Variable           " + While.Variable(someData.Count) + Environment.NewLine);
            sb.Append("while Property           " + While.Property(someData) + Environment.NewLine);

            sb.Append("do While Variable        " + DoWhile.Variable(someData.Count) + Environment.NewLine);
            sb.Append("do While Property        " + DoWhile.Property(someData) + Environment.NewLine);

            sb.Append("foreach A                " + Foreach.Test(someData) + Environment.NewLine);
            sb.Append("foreach B                " + Foreach.BasicTestOne(someData) + Environment.NewLine);
            sb.Append("foreach C                " + Foreach.BasicTestTwo(someData) + Environment.NewLine);

            sb.Append("parallel Foreach         " + ParallelForEach.Test(someData) + Environment.NewLine);

            sb.Append("foreach Linq A           " + ForeachLinq.Test(someData) + Environment.NewLine);
            sb.Append("foreach Linq B           " + ForeachLinq.BasicForEach(someData) + Environment.NewLine);


            System.IO.File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\loop.txt", sb.ToString());



            //set break point in CustomEnumerator method and debug
            var list = new CustomEnumerator();

            foreach (Person item in list)
            {
            }


            Console.ReadLine();
        }
コード例 #8
0
        private static ActivityView BuildParallelForEachView <T>(ParallelForEach <T> source)
        {
            string activityId = ObjectIdManager.GetId(source);

            string values   = ExpressionConvert.ToString(source.Values);
            string argument = string.Empty;

            if (source.Body != null)
            {
                argument = source.Body.Argument.Name;
            }

            var view = new ForEachView(activityId)
            {
                ActivityName = source.DisplayName,
                Values       = values,
                Argument     = argument,
            };

            return(view);
        }
コード例 #9
0
    public static void Explain(this ParallelForEach runnable, TextWriter writer)
    {
        writer.WriteLine(@"
- Similar to `Parallel.For` but this time foreach
");
    }