diff --git a/MaddoShared/ImageCreationStuff.cs b/MaddoShared/ImageCreationStuff.cs index 439f3dc..e8a8a3e 100644 --- a/MaddoShared/ImageCreationStuff.cs +++ b/MaddoShared/ImageCreationStuff.cs @@ -9,10 +9,11 @@ using System.Threading; using System.Threading.Tasks; using CatalogVbLib; using Dasync.Collections; +using Microsoft.Extensions.Logging; namespace MaddoShared { - public class ImageCreationStuff + public class ImageCreationStuff(ILogger logger) { public class Options { @@ -31,7 +32,9 @@ namespace MaddoShared public bool LinearExecution { get; set; } } - public async Task CreaCatalogoParallel(Options options, ConcurrentBag results, EventHandler> updateEvent, CancellationToken cancellationToken = default(CancellationToken)) + public async Task CreaCatalogoParallel(Options options, ConcurrentBag results, + EventHandler> updateEvent, + CancellationToken cancellationToken = default(CancellationToken)) { var stopwatch = new Stopwatch(); stopwatch.Start(); @@ -42,18 +45,20 @@ namespace MaddoShared // todo set finito label stopwatch.Stop(); - return $"{stopwatch.Elapsed.Hours}h {stopwatch.Elapsed.Minutes}m ${stopwatch.Elapsed.Seconds}s ({stopwatch.Elapsed.TotalSeconds}s)"; - - + return + $"{stopwatch.Elapsed.Hours}h {stopwatch.Elapsed.Minutes}m ${stopwatch.Elapsed.Seconds}s ({stopwatch.Elapsed.TotalSeconds}s)"; } - public async Task CreaImmaginiParallel(Options options, ConcurrentBag results, EventHandler> updateEvent, CancellationToken cancellationToken = default(CancellationToken)) + public async Task CreaImmaginiParallel(Options options, ConcurrentBag results, + EventHandler> updateEvent, + CancellationToken cancellationToken = default(CancellationToken)) { var dataToProcess = new List(); if (options.AggiornaSottodirectory && options.CreaSottocartelle) { var helper = new FileHelperSharp(); - dataToProcess = helper.GetFilesRecursive(new DirectoryInfo(options.SourcePath), new DirectoryInfo(options.DestinationPath), + dataToProcess = helper.GetFilesRecursive(new DirectoryInfo(options.SourcePath), + new DirectoryInfo(options.DestinationPath), "*.jpg", new FileHelperOptions() { FilesPerFolder = options.FilePerCartella, @@ -106,14 +111,19 @@ namespace MaddoShared //var allTasks = new List(); var test = from d in dataToProcess - select Task.Factory.StartNew(async () => - { - await new ImageCreatorSharp(d.File, d.Directory).CreaImmagineThread(d.File.Name); + select Task.Factory.StartNew(async () => + { + var imgCreator = new ImageCreatorSharp(d.File, d.Directory); + await imgCreator.CreaImmagineThread(d.File.Name); + + //await new ImageCreatorSharp(d.File, d.Directory).CreaImmagineThread(d.File.Name); - //var imgC = new ImageCreatorSharp(d.File, d.Directory); - //imgC.CreaImmagineThread(d.File.Name); - //imgC = null; - }, CancellationToken.None, TaskCreationOptions.None, scheduler); + imgCreator = null; + + //var imgC = new ImageCreatorSharp(d.File, d.Directory); + //imgC.CreaImmagineThread(d.File.Name); + //imgC = null; + }, CancellationToken.None, TaskCreationOptions.None, scheduler); //int count = 0; @@ -128,59 +138,66 @@ namespace MaddoShared { if (options.ChunksSize == 0) { - - //var opts = new ParallelOptions() { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken, TaskScheduler = scheduler}; await dataToProcess.ParallelForEachAsync(async fileData => - { - await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name); - results.Add(fileData.File.Name); - //count = Interlocked.Increment(ref count); - try - { - updateEvent?.Invoke(this, new Tuple(fileData.File.Name, dataToProcess.Count) ); - } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } - - }, maxDegreeOfParallelism: threads, false, cancellationToken); - + { + var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); + await imgCreator.CreaImmagineThread(fileData.File.Name); + // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name); + results.Add(fileData.File.Name); + //count = Interlocked.Increment(ref count); + try + { + updateEvent?.Invoke(this, new Tuple(fileData.File.Name, dataToProcess.Count)); + } + catch (Exception e) + { + logger.LogError(e, "Error in reporting update"); + //Console.WriteLine(e); + throw; + } + finally + { + // Disposing of the creator + imgCreator = null; + } + }, maxDegreeOfParallelism: threads, false, cancellationToken); } else { - var asdf = SplitList(dataToProcess.ToList(), options.ChunksSize).ToList(); - + foreach (var sdaf in asdf) { await sdaf.ParallelForEachAsync(async fileData => { - await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name); + var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); + await imgCreator.CreaImmagineThread(fileData.File.Name); + // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread( + // fileData.File.Name); results.Add(fileData.File.Name); //count = Interlocked.Increment(ref count); try { - updateEvent?.Invoke(this, new Tuple(fileData.File.Name, dataToProcess.Count)); + updateEvent?.Invoke(this, + new Tuple(fileData.File.Name, dataToProcess.Count)); } catch (Exception e) { - Console.WriteLine(e); + logger.LogError("Error in reporting update"); + //Console.WriteLine(e); throw; } + finally + { + imgCreator = null; + } }, maxDegreeOfParallelism: threads, false, cancellationToken); } - - - } } - - } public static IEnumerable> SplitList(List bigList, int nSize = 3) @@ -191,4 +208,4 @@ namespace MaddoShared } } } -} +} \ No newline at end of file diff --git a/MaddoShared/MaddoShared.csproj b/MaddoShared/MaddoShared.csproj index 25053f8..4ba12c1 100644 --- a/MaddoShared/MaddoShared.csproj +++ b/MaddoShared/MaddoShared.csproj @@ -15,6 +15,7 @@ + diff --git a/imagecatalog/DataModel.cs b/imagecatalog/DataModel.cs index 4725554..da345bf 100644 --- a/imagecatalog/DataModel.cs +++ b/imagecatalog/DataModel.cs @@ -17,6 +17,8 @@ namespace ImageCatalog_2 public ICommand TestCommand { get; } public ICommand AsyncTestCommand { get; } + + public ICommand ProcessImagesCommand { get; } private readonly ITestService _service; public DataModel(ITestService testService) @@ -25,6 +27,8 @@ namespace ImageCatalog_2 TestCommand = new RelayCommand(Test); AsyncTestCommand = new AsyncCommand(TestAsync); + + ProcessImagesCommand = new AsyncCommand(ProcessImages); } private string _sourcePath; @@ -73,5 +77,10 @@ namespace ImageCatalog_2 Debug.WriteLine("Yep c"); } + private async Task ProcessImages() + { + + } + } } diff --git a/imagecatalog/MainForm.cs b/imagecatalog/MainForm.cs index f856f2a..d3e5e3c 100644 --- a/imagecatalog/MainForm.cs +++ b/imagecatalog/MainForm.cs @@ -32,9 +32,12 @@ namespace ImageCatalog private readonly ITestService _service; private readonly ILogger _logger; - public MainForm(ITestService testService, ILogger logger) + private readonly ImageCreationStuff _imageCreationService; + + public MainForm(ITestService testService, ImageCreationStuff imageCreationStuff, ILogger logger) { _service = testService; + _imageCreationService = imageCreationStuff; _logger = logger; _logger.LogDebug("Start"); @@ -254,6 +257,7 @@ namespace ImageCatalog //btnCreaCatalogoAsync.Enabled = true; } + // Vecchio, non usato private void btnCreaCatalogo_Click(object sender, EventArgs e) { _logger.LogInformation("Avvio elaborazione..."); @@ -1573,14 +1577,14 @@ namespace ImageCatalog private async void Button1_Click(object sender, EventArgs e) { + _logger.LogInformation("Avvio elaborazione..."); lockUI(); // Dim timeStart As Date // Dim timeStop As Date _mainToken?.Dispose(); _mainToken = new CancellationTokenSource(); var token = _mainToken.Token; - - + // timeStart = TimeOfDay FixPaths(); Label10.Text = "Elaborazione in corso..."; @@ -1593,24 +1597,25 @@ namespace ImageCatalog ProgressBar1.Value = 0; // Await CreaCatalogoParallel() - var imgStf = new ImageCreationStuff(); - var imageCreationOptions = new ImageCreationStuff.Options(); - imageCreationOptions.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked; - imageCreationOptions.CreaSottocartelle = chkCreaSottocartelle.Checked; - imageCreationOptions.FilePerCartella = Conversions.ToInteger(txtFilePerCartella.Text); - imageCreationOptions.SuffissoCartelle = txtSuffissoCartelle.Text; - imageCreationOptions.CifreContatore = Conversions.ToInteger(txtCifreContatore.Text); - imageCreationOptions.NumerazioneType = GetNumerazioneEnum(); - imageCreationOptions.SourcePath = Model.SourcePath; - imageCreationOptions.DestinationPath = Model.DestinationPath; - imageCreationOptions.MaxThreads = Conversions.ToInteger(TextBox7.Text); - imageCreationOptions.ChunksSize = Conversions.ToInteger(TextBox8.Text); - imageCreationOptions.LinearExecution = rdbVecchioMetodo.Checked; - + var imgStf = _imageCreationService; //new ImageCreationStuff(); + var imageCreationOptions = new ImageCreationStuff.Options + { + AggiornaSottodirectory = chkAggiornaSottodirectory.Checked, + CreaSottocartelle = chkCreaSottocartelle.Checked, + FilePerCartella = Conversions.ToInteger(txtFilePerCartella.Text), + SuffissoCartelle = txtSuffissoCartelle.Text, + CifreContatore = Conversions.ToInteger(txtCifreContatore.Text), + NumerazioneType = GetNumerazioneEnum(), + SourcePath = Model.SourcePath, + DestinationPath = Model.DestinationPath, + MaxThreads = Conversions.ToInteger(TextBox7.Text), + ChunksSize = Conversions.ToInteger(TextBox8.Text), + LinearExecution = rdbVecchioMetodo.Checked + }; try { - _results = new ConcurrentBag(); + _results = []; _currentAmount = 0; _previousAmount = 0; timer1.Tick += Timer1OnTick; @@ -1661,7 +1666,7 @@ namespace ImageCatalog var timeStart = DateAndTime.TimeOfDay; ContaImmaginiThread = 0; setLabel10Text("Elaborazione in corso..."); - var imgStf = new ImageCreationStuff(); + var imgStf = _imageCreationService; var imageCreationOptions = new ImageCreationStuff.Options(); imageCreationOptions.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked; imageCreationOptions.CreaSottocartelle = chkCreaSottocartelle.Checked; diff --git a/imagecatalog/Program.cs b/imagecatalog/Program.cs index 08949f6..0fe5fba 100644 --- a/imagecatalog/Program.cs +++ b/imagecatalog/Program.cs @@ -1,6 +1,7 @@ using System.Runtime.InteropServices; using ImageCatalog; using ImageCatalog_2.Services; +using MaddoShared; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -80,6 +81,8 @@ namespace ImageCatalog_2 services.AddTransient(); + services.AddTransient(); + // Register your forms services.AddTransient();