Make detection of archive be multithread.

This commit is contained in:
2025-07-23 14:55:09 +01:00
parent 743b49c4f0
commit cf0f79338a

View File

@@ -1,9 +1,11 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using RomRepoMgr.Core.Aaru;
@@ -83,10 +85,11 @@ public sealed class FileImporter(bool onlyKnown, bool deleteAfterImport)
Maximum = Files.Count
});
List<string> files = [];
Archives = [];
ConcurrentBag<string> files = [];
ConcurrentBag<string> archives = [];
foreach(string file in Files)
Parallel.ForEach(Files,
file =>
{
try
{
@@ -100,7 +103,7 @@ public sealed class FileImporter(bool onlyKnown, bool deleteAfterImport)
new MessageEventArgs
{
Message = "Checking archives. Found " +
Archives.Count +
archives.Count +
" archives and " +
files.Count +
" files."
@@ -109,8 +112,8 @@ public sealed class FileImporter(bool onlyKnown, bool deleteAfterImport)
SetMessage2?.Invoke(this,
new MessageEventArgs
{
Message = string.Format("Checking if file {0} is an archive...",
Path.GetFileName(file))
Message =
$"Checking if file {Path.GetFileName(file)} is an archive..."
});
SetIndeterminateProgress2?.Invoke(this, System.EventArgs.Empty);
@@ -121,19 +124,22 @@ public sealed class FileImporter(bool onlyKnown, bool deleteAfterImport)
if(archiveFormat != null && FAT.Identify(file)) archiveFormat = null;
if(archiveFormat != null)
Archives.Add(file);
archives.Add(file);
else
files.Add(file);
_position++;
Interlocked.Increment(ref _position);
}
catch(Exception ex)
{
Console.WriteLine("Exception while checking file {0}: {1}", Path.GetFileName(file), ex.Message);
}
Console.WriteLine("Exception while checking file {0}: {1}",
Path.GetFileName(file),
ex.Message);
}
});
Files = files;
Files = files.ToList();
Archives = archives.ToList();
SetMessage?.Invoke(this,
new MessageEventArgs