diff --git a/ConsoleInterface/Program.cs b/ConsoleInterface/Program.cs index 49617d7..da529ea 100644 --- a/ConsoleInterface/Program.cs +++ b/ConsoleInterface/Program.cs @@ -9,7 +9,7 @@ namespace ConsoleInterface { var loggerFactory = LoggerFactory.Create(b => b.AddConsole()); var outputFormatter = OriginalFilenameOutputFormatter.Create(@"C:\Users\nutiu\Downloads\Photos-001\clean"); - var executor = TasksExecutor.Create(); + var executor = TaskExecutor.Create(); executor.Logger = loggerFactory.CreateLogger("Executor"); executor.ParallelCleanImages(new[] { diff --git a/Image/TasksExecutor.cs b/Image/TaskExecutor.cs similarity index 55% rename from Image/TasksExecutor.cs rename to Image/TaskExecutor.cs index ecc9745..127ca17 100644 --- a/Image/TasksExecutor.cs +++ b/Image/TaskExecutor.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -6,28 +7,37 @@ using Microsoft.Extensions.Logging.Abstractions; namespace Image { - public class TasksExecutor + public class TaskExecutor { public ILogger Logger = NullLogger.Instance; - public static TasksExecutor Create() + public static TaskExecutor Create() { - return new TasksExecutor(); + return new TaskExecutor(); } - public void CleanImage(string fileName, string newFilename) + public bool CleanImage(string fileName, string newFilename) { - ICleaner cleaner = new Cleaner(fileName); - cleaner.CleanImage(newFilename); + try + { + ICleaner cleaner = new Cleaner(fileName); + cleaner.CleanImage(newFilename); + return true; + } + catch (Exception e) + { + Logger.LogError(e.ToString()); + return false; + } } public void ParallelCleanImages(IEnumerable fileNames, IOutputFormatter outputFormatter) { Logger.LogInformation("Starting parallel image cleaning."); - var tasks = new List(); + var tasks = new List>(); foreach (var fileName in fileNames) { - var task = new Task(() => { CleanImage(fileName, outputFormatter.FormatOutputPath(fileName)); }); + var task = new Task(() => CleanImage(fileName, outputFormatter.FormatOutputPath(fileName))); tasks.Add(task); task.Start(); } @@ -35,7 +45,7 @@ namespace Image var result = Task.WhenAll(tasks); result.Wait(); - var successTasks = tasks.Count(t => t.IsCompletedSuccessfully); + var successTasks = tasks.Count(t => t.IsCompletedSuccessfully && t.Result); var errorTasks = tasks.Count() - successTasks; Logger.LogInformation($"All tasks completed. Success: {successTasks}, Errors: {errorTasks}"); }