Parallel Programming- For Method As For Loop C# with Example



Parallel Programming- For Method As For Loop C# with Example

 using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CSharpMultiThreading
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		
		private void btnProcessImages_Click(object sender, EventArgs e)
		{
			ProcessFiles();
		}
		
		private void ProcessFiles()
		{
			// Load up all *.jpg files, and make a new folder for the modified data.
			string[] files = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg", SearchOption.AllDirectories);
			string newDir = @"C:\ModifiedPictures";
			Directory.CreateDirectory(newDir);
			
			// Process the image data in a blocking manner.
			foreach (string currentFile in files)
			{
				string filename = Path.GetFileName(currentFile);
				using (Bitmap bitmap = new Bitmap(currentFile))
				{
					bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
					bitmap.Save(Path.Combine(newDir, filename));
			
					// Print out the ID of the thread processing the current image.
					this.Text = string.Format("Processing {0} on thread {1}", filename,
					Thread.CurrentThread.ManagedThreadId);
				}
			}
		}
	}
} 

0 Comment's

Comment Form