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