optimizations

This commit is contained in:
Marco 2025-07-23 17:16:06 +02:00
commit b98623f092
5 changed files with 97 additions and 62 deletions

View file

@ -9,10 +9,11 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CatalogVbLib; using CatalogVbLib;
using Dasync.Collections; using Dasync.Collections;
using Microsoft.Extensions.Logging;
namespace MaddoShared namespace MaddoShared
{ {
public class ImageCreationStuff public class ImageCreationStuff(ILogger<ImageCreationStuff> logger)
{ {
public class Options public class Options
{ {
@ -31,7 +32,9 @@ namespace MaddoShared
public bool LinearExecution { get; set; } public bool LinearExecution { get; set; }
} }
public async Task<string> CreaCatalogoParallel(Options options, ConcurrentBag<string> results, EventHandler<Tuple<string,int>> updateEvent, CancellationToken cancellationToken = default(CancellationToken)) public async Task<string> CreaCatalogoParallel(Options options, ConcurrentBag<string> results,
EventHandler<Tuple<string, int>> updateEvent,
CancellationToken cancellationToken = default(CancellationToken))
{ {
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@ -42,18 +45,20 @@ namespace MaddoShared
// todo set finito label // todo set finito label
stopwatch.Stop(); 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<string> results, EventHandler<Tuple<string, int>> updateEvent, CancellationToken cancellationToken = default(CancellationToken)) public async Task CreaImmaginiParallel(Options options, ConcurrentBag<string> results,
EventHandler<Tuple<string, int>> updateEvent,
CancellationToken cancellationToken = default(CancellationToken))
{ {
var dataToProcess = new List<FileData>(); var dataToProcess = new List<FileData>();
if (options.AggiornaSottodirectory && options.CreaSottocartelle) if (options.AggiornaSottodirectory && options.CreaSottocartelle)
{ {
var helper = new FileHelperSharp(); 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() "*.jpg", new FileHelperOptions()
{ {
FilesPerFolder = options.FilePerCartella, FilesPerFolder = options.FilePerCartella,
@ -106,14 +111,19 @@ namespace MaddoShared
//var allTasks = new List<Task>(); //var allTasks = new List<Task>();
var test = from d in dataToProcess var test = from d in dataToProcess
select Task.Factory.StartNew(async () => select Task.Factory.StartNew(async () =>
{ {
await new ImageCreatorSharp(d.File, d.Directory).CreaImmagineThread(d.File.Name); var imgCreator = new ImageCreatorSharp(d.File, d.Directory);
await imgCreator.CreaImmagineThread(d.File.Name);
//var imgC = new ImageCreatorSharp(d.File, d.Directory); //await new ImageCreatorSharp(d.File, d.Directory).CreaImmagineThread(d.File.Name);
//imgC.CreaImmagineThread(d.File.Name);
//imgC = null; imgCreator = null;
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
//var imgC = new ImageCreatorSharp(d.File, d.Directory);
//imgC.CreaImmagineThread(d.File.Name);
//imgC = null;
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
//int count = 0; //int count = 0;
@ -128,31 +138,34 @@ namespace MaddoShared
{ {
if (options.ChunksSize == 0) if (options.ChunksSize == 0)
{ {
//var opts = new ParallelOptions() { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken, TaskScheduler = scheduler}; //var opts = new ParallelOptions() { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken, TaskScheduler = scheduler};
await dataToProcess.ParallelForEachAsync(async fileData => await dataToProcess.ParallelForEachAsync(async fileData =>
{ {
await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name); var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory);
results.Add(fileData.File.Name); await imgCreator.CreaImmagineThread(fileData.File.Name);
//count = Interlocked.Increment(ref count); // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name);
try results.Add(fileData.File.Name);
{ //count = Interlocked.Increment(ref count);
updateEvent?.Invoke(this, new Tuple<string, int>(fileData.File.Name, dataToProcess.Count) );
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}, maxDegreeOfParallelism: threads, false, cancellationToken);
try
{
updateEvent?.Invoke(this, new Tuple<string, int>(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 else
{ {
var asdf = SplitList(dataToProcess.ToList(), options.ChunksSize).ToList(); var asdf = SplitList(dataToProcess.ToList(), options.ChunksSize).ToList();
@ -160,27 +173,31 @@ namespace MaddoShared
{ {
await sdaf.ParallelForEachAsync(async fileData => 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); results.Add(fileData.File.Name);
//count = Interlocked.Increment(ref count); //count = Interlocked.Increment(ref count);
try try
{ {
updateEvent?.Invoke(this, new Tuple<string, int>(fileData.File.Name, dataToProcess.Count)); updateEvent?.Invoke(this,
new Tuple<string, int>(fileData.File.Name, dataToProcess.Count));
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); logger.LogError("Error in reporting update");
//Console.WriteLine(e);
throw; throw;
} }
finally
{
imgCreator = null;
}
}, maxDegreeOfParallelism: threads, false, cancellationToken); }, maxDegreeOfParallelism: threads, false, cancellationToken);
} }
} }
} }
} }
public static IEnumerable<List<T>> SplitList<T>(List<T> bigList, int nSize = 3) public static IEnumerable<List<T>> SplitList<T>(List<T> bigList, int nSize = 3)

View file

@ -15,6 +15,7 @@
<PackageReference Include="Ben.Demystifier" Version="0.4.1" /> <PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.6" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.6" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.7" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" /> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
<PackageReference Include="System.Buffers" Version="4.6.1" /> <PackageReference Include="System.Buffers" Version="4.6.1" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.6" /> <PackageReference Include="System.Collections.Immutable" Version="9.0.6" />

View file

@ -18,6 +18,8 @@ namespace ImageCatalog_2
public ICommand AsyncTestCommand { get; } public ICommand AsyncTestCommand { get; }
public ICommand ProcessImagesCommand { get; }
private readonly ITestService _service; private readonly ITestService _service;
public DataModel(ITestService testService) public DataModel(ITestService testService)
{ {
@ -25,6 +27,8 @@ namespace ImageCatalog_2
TestCommand = new RelayCommand(Test); TestCommand = new RelayCommand(Test);
AsyncTestCommand = new AsyncCommand(TestAsync); AsyncTestCommand = new AsyncCommand(TestAsync);
ProcessImagesCommand = new AsyncCommand(ProcessImages);
} }
private string _sourcePath; private string _sourcePath;
@ -73,5 +77,10 @@ namespace ImageCatalog_2
Debug.WriteLine("Yep c"); Debug.WriteLine("Yep c");
} }
private async Task ProcessImages()
{
}
} }
} }

View file

@ -32,9 +32,12 @@ namespace ImageCatalog
private readonly ITestService _service; private readonly ITestService _service;
private readonly ILogger<MainForm> _logger; private readonly ILogger<MainForm> _logger;
public MainForm(ITestService testService, ILogger<MainForm> logger) private readonly ImageCreationStuff _imageCreationService;
public MainForm(ITestService testService, ImageCreationStuff imageCreationStuff, ILogger<MainForm> logger)
{ {
_service = testService; _service = testService;
_imageCreationService = imageCreationStuff;
_logger = logger; _logger = logger;
_logger.LogDebug("Start"); _logger.LogDebug("Start");
@ -254,6 +257,7 @@ namespace ImageCatalog
//btnCreaCatalogoAsync.Enabled = true; //btnCreaCatalogoAsync.Enabled = true;
} }
// Vecchio, non usato
private void btnCreaCatalogo_Click(object sender, EventArgs e) private void btnCreaCatalogo_Click(object sender, EventArgs e)
{ {
_logger.LogInformation("Avvio elaborazione..."); _logger.LogInformation("Avvio elaborazione...");
@ -1573,6 +1577,7 @@ namespace ImageCatalog
private async void Button1_Click(object sender, EventArgs e) private async void Button1_Click(object sender, EventArgs e)
{ {
_logger.LogInformation("Avvio elaborazione...");
lockUI(); lockUI();
// Dim timeStart As Date // Dim timeStart As Date
// Dim timeStop As Date // Dim timeStop As Date
@ -1580,7 +1585,6 @@ namespace ImageCatalog
_mainToken = new CancellationTokenSource(); _mainToken = new CancellationTokenSource();
var token = _mainToken.Token; var token = _mainToken.Token;
// timeStart = TimeOfDay // timeStart = TimeOfDay
FixPaths(); FixPaths();
Label10.Text = "Elaborazione in corso..."; Label10.Text = "Elaborazione in corso...";
@ -1593,24 +1597,25 @@ namespace ImageCatalog
ProgressBar1.Value = 0; ProgressBar1.Value = 0;
// Await CreaCatalogoParallel() // Await CreaCatalogoParallel()
var imgStf = new ImageCreationStuff(); var imgStf = _imageCreationService; //new ImageCreationStuff();
var imageCreationOptions = new ImageCreationStuff.Options(); var imageCreationOptions = new ImageCreationStuff.Options
imageCreationOptions.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked; {
imageCreationOptions.CreaSottocartelle = chkCreaSottocartelle.Checked; AggiornaSottodirectory = chkAggiornaSottodirectory.Checked,
imageCreationOptions.FilePerCartella = Conversions.ToInteger(txtFilePerCartella.Text); CreaSottocartelle = chkCreaSottocartelle.Checked,
imageCreationOptions.SuffissoCartelle = txtSuffissoCartelle.Text; FilePerCartella = Conversions.ToInteger(txtFilePerCartella.Text),
imageCreationOptions.CifreContatore = Conversions.ToInteger(txtCifreContatore.Text); SuffissoCartelle = txtSuffissoCartelle.Text,
imageCreationOptions.NumerazioneType = GetNumerazioneEnum(); CifreContatore = Conversions.ToInteger(txtCifreContatore.Text),
imageCreationOptions.SourcePath = Model.SourcePath; NumerazioneType = GetNumerazioneEnum(),
imageCreationOptions.DestinationPath = Model.DestinationPath; SourcePath = Model.SourcePath,
imageCreationOptions.MaxThreads = Conversions.ToInteger(TextBox7.Text); DestinationPath = Model.DestinationPath,
imageCreationOptions.ChunksSize = Conversions.ToInteger(TextBox8.Text); MaxThreads = Conversions.ToInteger(TextBox7.Text),
imageCreationOptions.LinearExecution = rdbVecchioMetodo.Checked; ChunksSize = Conversions.ToInteger(TextBox8.Text),
LinearExecution = rdbVecchioMetodo.Checked
};
try try
{ {
_results = new ConcurrentBag<string>(); _results = [];
_currentAmount = 0; _currentAmount = 0;
_previousAmount = 0; _previousAmount = 0;
timer1.Tick += Timer1OnTick; timer1.Tick += Timer1OnTick;
@ -1661,7 +1666,7 @@ namespace ImageCatalog
var timeStart = DateAndTime.TimeOfDay; var timeStart = DateAndTime.TimeOfDay;
ContaImmaginiThread = 0; ContaImmaginiThread = 0;
setLabel10Text("Elaborazione in corso..."); setLabel10Text("Elaborazione in corso...");
var imgStf = new ImageCreationStuff(); var imgStf = _imageCreationService;
var imageCreationOptions = new ImageCreationStuff.Options(); var imageCreationOptions = new ImageCreationStuff.Options();
imageCreationOptions.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked; imageCreationOptions.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked;
imageCreationOptions.CreaSottocartelle = chkCreaSottocartelle.Checked; imageCreationOptions.CreaSottocartelle = chkCreaSottocartelle.Checked;

View file

@ -1,6 +1,7 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ImageCatalog; using ImageCatalog;
using ImageCatalog_2.Services; using ImageCatalog_2.Services;
using MaddoShared;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -80,6 +81,8 @@ namespace ImageCatalog_2
services.AddTransient<DataModel>(); services.AddTransient<DataModel>();
services.AddTransient<ImageCreationStuff>();
// Register your forms // Register your forms
services.AddTransient<MainForm>(); services.AddTransient<MainForm>();