mirror of
https://github.com/claunia/romrepomgr.git
synced 2025-12-16 19:24:51 +00:00
[App] Refactor importing DAT folder to use parallel foreach.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
@@ -22,8 +21,6 @@ public partial class DatImporter : ObservableObject
|
||||
[ObservableProperty]
|
||||
string _statusMessage;
|
||||
public string Filename { get; internal init; }
|
||||
public Task Task { get; set; }
|
||||
public bool Running { get; private set; } = true;
|
||||
|
||||
internal void OnErrorOccurred(object sender, ErrorEventArgs e) => Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
@@ -59,6 +56,5 @@ public partial class DatImporter : ObservableObject
|
||||
Minimum = 0;
|
||||
Progress = 1;
|
||||
StatusMessage = e.Message;
|
||||
Running = false;
|
||||
});
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Avalonia.Controls;
|
||||
@@ -50,7 +51,6 @@ public sealed partial class ImportDatFolderViewModel : ViewModelBase
|
||||
bool _recursiveChecked;
|
||||
[ObservableProperty]
|
||||
string _statusMessage;
|
||||
int _workers;
|
||||
|
||||
public ImportDatFolderViewModel()
|
||||
{
|
||||
@@ -100,44 +100,39 @@ public sealed partial class ImportDatFolderViewModel : ViewModelBase
|
||||
CanStart = false;
|
||||
IsReady = false;
|
||||
IsImporting = true;
|
||||
_workers = 0;
|
||||
_stopwatch.Restart();
|
||||
|
||||
Import();
|
||||
_ = Task.Run(Import);
|
||||
}
|
||||
|
||||
void Import()
|
||||
{
|
||||
Parallel.ForEach(_datFiles,
|
||||
new ParallelOptions
|
||||
{
|
||||
MaxDegreeOfParallelism = Environment.ProcessorCount
|
||||
},
|
||||
datFile =>
|
||||
{
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
if(_listPosition >= _datFiles.Length)
|
||||
{
|
||||
if(_workers != 0) return;
|
||||
StatusMessage = string.Format(Localization.ImportingItem, Path.GetFileName(datFile));
|
||||
|
||||
ProgressVisible = false;
|
||||
StatusMessage = Localization.Finished;
|
||||
CanClose = true;
|
||||
CanStart = false;
|
||||
IsReady = true;
|
||||
_stopwatch.Stop();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
StatusMessage = string.Format(Localization.ImportingItem, Path.GetFileName(_datFiles[_listPosition]));
|
||||
ProgressValue = _listPosition;
|
||||
});
|
||||
|
||||
var model = new DatImporter
|
||||
{
|
||||
Filename = Path.GetFileName(_datFiles[_listPosition]),
|
||||
Filename = Path.GetFileName(datFile),
|
||||
Minimum = 0,
|
||||
Maximum = _datFiles.Length,
|
||||
Progress = 0,
|
||||
Indeterminate = false
|
||||
};
|
||||
|
||||
var worker =
|
||||
new Core.Workers.DatImporter(_datFiles[_listPosition], Category, new SerilogLoggerFactory(Log.Logger));
|
||||
var worker = new Core.Workers.DatImporter(datFile,
|
||||
Category,
|
||||
new SerilogLoggerFactory(Log.Logger));
|
||||
|
||||
worker.ErrorOccurred += model.OnErrorOccurred;
|
||||
worker.SetIndeterminateProgress += model.OnSetIndeterminateProgress;
|
||||
@@ -147,28 +142,21 @@ public sealed partial class ImportDatFolderViewModel : ViewModelBase
|
||||
worker.WorkFinished += model.OnWorkFinished;
|
||||
worker.RomSetAdded += RomSetAdded;
|
||||
|
||||
worker.WorkFinished += (_, _) =>
|
||||
Dispatcher.UIThread.Post(() => Importers.Add(model));
|
||||
|
||||
worker.Import();
|
||||
|
||||
Interlocked.Increment(ref _listPosition);
|
||||
});
|
||||
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
_workers--;
|
||||
|
||||
if(_workers < Environment.ProcessorCount) Import();
|
||||
};
|
||||
|
||||
worker.ErrorOccurred += (_, _) =>
|
||||
{
|
||||
_workers--;
|
||||
|
||||
if(_workers < Environment.ProcessorCount) Import();
|
||||
};
|
||||
|
||||
Importers.Add(model);
|
||||
|
||||
model.Task = Task.Run(worker.Import);
|
||||
|
||||
_workers++;
|
||||
_listPosition++;
|
||||
|
||||
if(_workers < Environment.ProcessorCount) Import();
|
||||
ProgressVisible = false;
|
||||
StatusMessage = Localization.Finished;
|
||||
CanClose = true;
|
||||
CanStart = false;
|
||||
IsReady = true;
|
||||
_stopwatch.Stop();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user