mirror of
https://github.com/rmcrackan/Libation.git
synced 2025-12-23 22:17:52 -05:00
Ensure FileSystemWatcher is disposed
This commit is contained in:
@@ -3,15 +3,16 @@ using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
#nullable enable
|
||||
namespace FileManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Tracks actual locations of files.
|
||||
/// </summary>
|
||||
public class BackgroundFileSystem
|
||||
/// <summary>
|
||||
/// Tracks actual locations of files.
|
||||
/// </summary>
|
||||
public class BackgroundFileSystem : IDisposable
|
||||
{
|
||||
public LongPath RootDirectory { get; private set; }
|
||||
public string SearchPattern { get; private set; }
|
||||
@@ -21,7 +22,7 @@ namespace FileManager
|
||||
private BlockingCollection<FileSystemEventArgs>? directoryChangesEvents { get; set; }
|
||||
private Task? backgroundScanner { get; set; }
|
||||
|
||||
private object fsCacheLocker { get; } = new();
|
||||
private Lock fsCacheLocker { get; } = new();
|
||||
private List<LongPath> fsCache { get; } = new();
|
||||
|
||||
public BackgroundFileSystem(LongPath rootDirectory, string searchPattern, SearchOption searchOptions)
|
||||
@@ -100,7 +101,6 @@ namespace FileManager
|
||||
|
||||
private void FileSystemWatcher_Error(object sender, ErrorEventArgs e)
|
||||
{
|
||||
Stop();
|
||||
Init();
|
||||
}
|
||||
|
||||
@@ -181,8 +181,12 @@ namespace FileManager
|
||||
fsCache.Add(newFile);
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
~BackgroundFileSystem() => Stop();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Stop();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ namespace LibationFileManager
|
||||
=> BookDirectoryFiles ??= newBookDirectoryFiles();
|
||||
|
||||
private static BackgroundFileSystem? BookDirectoryFiles { get; set; }
|
||||
private static object bookDirectoryFilesLocker { get; } = new();
|
||||
private static Lock bookDirectoryFilesLocker { get; } = new();
|
||||
private static EnumerationOptions enumerationOptions { get; } = new()
|
||||
{
|
||||
RecurseSubdirectories = true,
|
||||
@@ -148,8 +148,13 @@ namespace LibationFileManager
|
||||
{
|
||||
// If user changed the BooksDirectory: reinitialize
|
||||
lock (bookDirectoryFilesLocker)
|
||||
{
|
||||
if (BooksDirectory != BookDirectoryFiles?.RootDirectory)
|
||||
{
|
||||
BookDirectoryFiles?.Dispose();
|
||||
BookDirectoryFiles = newBookDirectoryFiles();
|
||||
}
|
||||
}
|
||||
|
||||
var regex = GetBookSearchRegex(productId);
|
||||
var diskFiles = BookDirectoryFiles?.FindFiles(regex) ?? [];
|
||||
@@ -167,9 +172,10 @@ namespace LibationFileManager
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
if (BookDirectoryFiles is null && BooksDirectory is not null)
|
||||
lock (bookDirectoryFilesLocker)
|
||||
BookDirectoryFiles = newBookDirectoryFiles();
|
||||
lock (bookDirectoryFilesLocker)
|
||||
{
|
||||
BookDirectoryFiles ??= newBookDirectoryFiles();
|
||||
}
|
||||
|
||||
BookDirectoryFiles?.RefreshFiles();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user