foreach as parallel c#

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;

public class Example
{
    public static void Main()
    {
        // A simple source for demonstration purposes. Modify this path as necessary.
        string[] files = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
        string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
        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 = Path.GetFileName(currentFile);
                                    var bitmap = new Bitmap(currentFile);

                                    bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    bitmap.Save(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 {filename} on thread {Thread.CurrentThread.ManagedThreadId}");
                                    //close lambda expression and method invocation
                                });

        // Keep the console window open in debug mode.
        Console.WriteLine("Processing complete. Press any key to exit.");
        Console.ReadKey();
    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to transform foreach into parallel foreach c# how to transform foreach into parallel foreach c#$ how to call different method in parallel.foreach in C# parallel foreach inside parallel foreach c# parallel.foreach options c# c# how to make entity parallel foreach c# entity parallel foreach foreach c# parallel for each parallel c# parallel foreach c# example parallel foreach c# with result parallel.foreach with result c# c# make foreach as parallel convert foreach to parallel.foreach c# C# Parallel.ForEach c# parallel foreach inside parallel foreach parallel foreach c# with return value Parallel.ForEach() c# parallel.foreach in c# parallel foreach good excmple c# .net parallel foreach Parallel foreach vb net example c# parallel foreach in order c# foreach loop parallel processing parallel foreach loop c# c# async parallel foreach c# using aait inside Parallel.Foreach r foreach dopar parallel.foreach example c# parallel for each C# c# .net parallel foreach c# parallel foreach when all parallel.foreach c# with two csharp foreach parallel c sharp parallel foreach how to use parallel foreach loop in c# parallel.foreach c# foreach parallel c# foreach as parallel c# foreach parallel c# parallel foreach configuration parallel foreach in parallel foreach c# parallel.foreach c# example how to multithread a foreach in C# parallel.foreach break c# parallel foreach c# context parallel foreach c# example long running parallel foreach c# async c# parallel foreach c# foreach in parallel accomulate c# foreach in parallel parallel.foreach parallel.foreach how to use it foreach is parallel vb.net parallel foreach c# parallel foreach example parallel foreach c# c# parallel for eac execute list of thread in aprallel c# parallel linq foreach c# parallel.foreach local finally execute action at end parallel foreach c#
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source