DEV Community

[Comment from a deleted post]
Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

If only there was a parallel keyword... Wait that would be stupid.

Collapse
 
jwp profile image
John Peters

Funny you mention that, in C# they do have a parallel keyword.

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
 });