diff --git a/PiwigoDirectorySync/Services/FileIndexer.cs b/PiwigoDirectorySync/Services/FileIndexer.cs index 386c131..18d3b89 100644 --- a/PiwigoDirectorySync/Services/FileIndexer.cs +++ b/PiwigoDirectorySync/Services/FileIndexer.cs @@ -1,4 +1,5 @@ -using System.Threading.Channels; +using System.Collections.Concurrent; +using System.Threading.Channels; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -9,9 +10,10 @@ namespace PiwigoDirectorySync.Services; internal class FileIndexer : IFileIndexer { - private readonly IList _failedFiles = new List(); + private readonly ConcurrentBag _failedFiles = new(); private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; + private int _totalFilesScanned; public FileIndexer(ILogger logger, IServiceProvider serviceProvider) { @@ -19,8 +21,9 @@ internal class FileIndexer : IFileIndexer _serviceProvider = serviceProvider; } - public int TotalFilesScanned { get; private set; } - public IReadOnlyCollection FailedFiles => _failedFiles.AsReadOnly(); + public int TotalFilesScanned => _totalFilesScanned; + + public IReadOnlyCollection FailedFiles => _failedFiles; public async Task StartProcessingAsync(Channel fileQueue, int piwigoServerId, CancellationToken ct) { @@ -74,7 +77,7 @@ internal class FileIndexer : IFileIndexer await db.SaveChangesAsync(ct); - TotalFilesScanned++; + Interlocked.Add(ref _totalFilesScanned, 1); } catch (Exception ex) {