optimizations
This commit is contained in:
parent
1b78b245d1
commit
b98623f092
5 changed files with 97 additions and 62 deletions
|
|
@ -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<ImageCreationStuff> logger)
|
||||
{
|
||||
public class Options
|
||||
{
|
||||
|
|
@ -31,7 +32,9 @@ namespace MaddoShared
|
|||
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();
|
||||
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<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>();
|
||||
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<Task>();
|
||||
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<string, int>(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<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
|
||||
{
|
||||
|
||||
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<string, int>(fileData.File.Name, dataToProcess.Count));
|
||||
updateEvent?.Invoke(this,
|
||||
new Tuple<string, int>(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<List<T>> SplitList<T>(List<T> bigList, int nSize = 3)
|
||||
|
|
@ -191,4 +208,4 @@ namespace MaddoShared
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue