1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Threading; using System.Drawing; namespace Parallel2 { class Program { public static void Main() { // A simple source for demonstration purposes. Modify this path as necessary. String[] files = System.IO.Directory.GetFiles(@"C:\Users\YellowFriend\Desktop\Pics\Digital-Flame\01", "*.jpg"); String newDir = @"C:\Users\YellowFriend\Desktop\Pics\Digital-Flame\01\Modified"; System.IO.Directory.CreateDirectory(newDir); // Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body) // Be sure to add a reference to System.Drawing.dll. 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); 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 {0} on thread {1}", filename, 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(); } } } |