locked
How to use Parallel.ForEach? RRS feed

  • Question

  • How to use Parallel.ForEach?

    I have written A worm ,but the percent of CPU is To high!I want to control the number of Threads .Please tell me how to do It


    Friday, September 11, 2015 4:31 PM

Answers

  • Hi Freeski,

    Do you have some code that you could show us to illustrate what you are trying to achieve? Or are you just talking in vague general terms about how you might go about writing some kind of code.

    Here is a MSDN example shows how to use a Parallel.ForEach loop to enable data parallelism over any System.Collections.IEnumerableor System.Collections.Generic.IEnumerable<T> data source.

    using System;
    using System.Drawing; // requires system.Drawing.dll 
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    
    class SimpleForEach
    {
        static void Main()
        {
            // A simple source for demonstration purposes. Modify this path as necessary. 
            string[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
            string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
            System.IO.Directory.CreateDirectory(newDir);
    
            //  Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
            Parallel.ForEach(files, currentFile =>
            {
                // The more computational work you do here, the greater  
                // the speedup compared to a sequential foreach loop. 
                string filename = System.IO.Path.GetFileName(currentFile);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile);
    
                bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                bitmap.Save(System.IO.Path.Combine(newDir, filename));
    
                // Peek behind the scenes to see how work is parallelized. 
                // But be aware: Thread contention for the Console slows down parallel loops!!!
                Console.WriteLine("Processing {0} on thread {1}", filename,
                                    Thread.CurrentThread.ManagedThreadId);
    
            } //close lambda expression
                 ); //close method invocation 
    
            // Keep the console window open in debug mode.
            Console.WriteLine("Processing complete. Press any key to exit.");
            Console.ReadKey();
        }
    }
    For more detailed information, please check the following link

    https://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 14, 2015 2:37 AM

All replies

  • "I have written A worm"

    A what?

    Saturday, September 12, 2015 6:17 AM
  • Hi Freeski,

    Do you have some code that you could show us to illustrate what you are trying to achieve? Or are you just talking in vague general terms about how you might go about writing some kind of code.

    Here is a MSDN example shows how to use a Parallel.ForEach loop to enable data parallelism over any System.Collections.IEnumerableor System.Collections.Generic.IEnumerable<T> data source.

    using System;
    using System.Drawing; // requires system.Drawing.dll 
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    
    class SimpleForEach
    {
        static void Main()
        {
            // A simple source for demonstration purposes. Modify this path as necessary. 
            string[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
            string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
            System.IO.Directory.CreateDirectory(newDir);
    
            //  Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
            Parallel.ForEach(files, currentFile =>
            {
                // The more computational work you do here, the greater  
                // the speedup compared to a sequential foreach loop. 
                string filename = System.IO.Path.GetFileName(currentFile);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile);
    
                bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                bitmap.Save(System.IO.Path.Combine(newDir, filename));
    
                // Peek behind the scenes to see how work is parallelized. 
                // But be aware: Thread contention for the Console slows down parallel loops!!!
                Console.WriteLine("Processing {0} on thread {1}", filename,
                                    Thread.CurrentThread.ManagedThreadId);
    
            } //close lambda expression
                 ); //close method invocation 
    
            // Keep the console window open in debug mode.
            Console.WriteLine("Processing complete. Press any key to exit.");
            Console.ReadKey();
        }
    }
    For more detailed information, please check the following link

    https://msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 14, 2015 2:37 AM