From 39d583120997abee7bf0395011f56b39e4d34699 Mon Sep 17 00:00:00 2001 From: Dave Chen Date: Fri, 28 Jun 2019 16:30:47 +0800 Subject: [PATCH] Using Task --- ImageResizer/ImageProcess.cs | 49 ++++++++++++++++++++++---------- ImageResizer/ImageResizer.csproj | 1 + ImageResizer/Program.cs | 7 +++-- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/ImageResizer/ImageProcess.cs b/ImageResizer/ImageProcess.cs index 6f83ee6..276a2df 100644 --- a/ImageResizer/ImageProcess.cs +++ b/ImageResizer/ImageProcess.cs @@ -1,8 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; +using System.Linq; +using System.Threading.Tasks; namespace ImageResizer { @@ -35,26 +38,42 @@ public void Clean(string destPath) /// 圖片來源目錄路徑 /// 產生圖片目的目錄路徑 /// 縮放比例 - public void ResizeImages(string sourcePath, string destPath, double scale) + public async Task ResizeImagesAsync(string sourcePath, string destPath, double scale) { + var allFiles = FindImages(sourcePath); - foreach (var filePath in allFiles) + Task[] tasks = new Task[allFiles.Count]; + for(int i = 0; i< allFiles.Count;i++) { - Image imgPhoto = Image.FromFile(filePath); - string imgName = Path.GetFileNameWithoutExtension(filePath); - - int sourceWidth = imgPhoto.Width; - int sourceHeight = imgPhoto.Height; + string s = allFiles[i]; + Console.WriteLine($"Run:{i}"); + tasks[i] = Task.Run(() => + { + Console.WriteLine($"TaskRun:"); + Image imgPhoto = Image.FromFile(s); + string imgName = Path.GetFileNameWithoutExtension(s); - int destionatonWidth = (int)(sourceWidth * scale); - int destionatonHeight = (int)(sourceHeight * scale); + int sourceWidth = imgPhoto.Width; + int sourceHeight = imgPhoto.Height; - Bitmap processedImage = processBitmap((Bitmap)imgPhoto, - sourceWidth, sourceHeight, - destionatonWidth, destionatonHeight); + int destionatonWidth = (int)(sourceWidth * scale); + int destionatonHeight = (int)(sourceHeight * scale); - string destFile = Path.Combine(destPath, imgName + ".jpg"); - processedImage.Save(destFile, ImageFormat.Jpeg); + Bitmap processedImage = processBitmap((Bitmap)imgPhoto, + sourceWidth, sourceHeight, + destionatonWidth, destionatonHeight); + string destFile = Path.Combine(destPath, imgName + ".jpg"); + processedImage.Save(destFile, ImageFormat.Jpeg); + Console.WriteLine($"TaskRunEnd:"); + return 1; + }); + }; + while (tasks.Length > 0) + { + Task task = await Task.WhenAny(tasks); + var temp = tasks.ToList(); + temp.Remove(task); // 將已經完成的工作,從清單中移除 + tasks = temp.ToArray(); } } diff --git a/ImageResizer/ImageResizer.csproj b/ImageResizer/ImageResizer.csproj index f04d447..af0a524 100644 --- a/ImageResizer/ImageResizer.csproj +++ b/ImageResizer/ImageResizer.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + latest AnyCPU diff --git a/ImageResizer/Program.cs b/ImageResizer/Program.cs index 63b06d5..778d266 100644 --- a/ImageResizer/Program.cs +++ b/ImageResizer/Program.cs @@ -9,7 +9,7 @@ namespace ImageResizer { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); ; @@ -20,10 +20,13 @@ static void Main(string[] args) Stopwatch sw = new Stopwatch(); sw.Start(); - imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); + await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); + Console.WriteLine("按下任一按鍵,結束處理程序"); + Console.ReadKey(); + } } }