dedicated lock objects for safety
This commit is contained in:
parent
d2eaf26117
commit
fd56017af5
@ -36,6 +36,7 @@ namespace FileManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static object bookDirectoryFilesLocker { get; } = new();
|
||||||
internal static BackgroundFileSystem BookDirectoryFiles { get; set; }
|
internal static BackgroundFileSystem BookDirectoryFiles { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -66,7 +67,7 @@ namespace FileManager
|
|||||||
//If user changed the BooksDirectory, reinitialize.
|
//If user changed the BooksDirectory, reinitialize.
|
||||||
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
|
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
|
||||||
{
|
{
|
||||||
lock (BookDirectoryFiles)
|
lock (bookDirectoryFilesLocker)
|
||||||
{
|
{
|
||||||
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
|
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -25,6 +25,8 @@ namespace FileManager
|
|||||||
private FileSystemWatcher fileSystemWatcher { get; set; }
|
private FileSystemWatcher fileSystemWatcher { get; set; }
|
||||||
private BlockingCollection<FileSystemEventArgs> directoryChangesEvents { get; set; }
|
private BlockingCollection<FileSystemEventArgs> directoryChangesEvents { get; set; }
|
||||||
private Task backgroundScanner { get; set; }
|
private Task backgroundScanner { get; set; }
|
||||||
|
|
||||||
|
private object fsCacheLocker { get; } = new();
|
||||||
private List<string> fsCache { get; } = new();
|
private List<string> fsCache { get; } = new();
|
||||||
|
|
||||||
public BackgroundFileSystem(string rootDirectory, string searchPattern, SearchOption searchOptions)
|
public BackgroundFileSystem(string rootDirectory, string searchPattern, SearchOption searchOptions)
|
||||||
@ -38,15 +40,13 @@ namespace FileManager
|
|||||||
|
|
||||||
public string FindFile(string regexPattern, RegexOptions options)
|
public string FindFile(string regexPattern, RegexOptions options)
|
||||||
{
|
{
|
||||||
lock (fsCache)
|
lock (fsCacheLocker)
|
||||||
{
|
|
||||||
return fsCache.FirstOrDefault(s => Regex.IsMatch(s, regexPattern, options));
|
return fsCache.FirstOrDefault(s => Regex.IsMatch(s, regexPattern, options));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RefreshFiles()
|
public void RefreshFiles()
|
||||||
{
|
{
|
||||||
lock (fsCache)
|
lock (fsCacheLocker)
|
||||||
{
|
{
|
||||||
fsCache.Clear();
|
fsCache.Clear();
|
||||||
fsCache.AddRange(Directory.EnumerateFiles(RootDirectory, SearchPattern, SearchOption));
|
fsCache.AddRange(Directory.EnumerateFiles(RootDirectory, SearchPattern, SearchOption));
|
||||||
@ -57,7 +57,7 @@ namespace FileManager
|
|||||||
{
|
{
|
||||||
Stop();
|
Stop();
|
||||||
|
|
||||||
lock (fsCache)
|
lock (fsCacheLocker)
|
||||||
fsCache.AddRange(Directory.EnumerateFiles(RootDirectory, SearchPattern, SearchOption));
|
fsCache.AddRange(Directory.EnumerateFiles(RootDirectory, SearchPattern, SearchOption));
|
||||||
|
|
||||||
directoryChangesEvents = new BlockingCollection<FileSystemEventArgs>();
|
directoryChangesEvents = new BlockingCollection<FileSystemEventArgs>();
|
||||||
@ -86,7 +86,7 @@ namespace FileManager
|
|||||||
//Dispose of directoryChangesEvents after backgroundScanner exists.
|
//Dispose of directoryChangesEvents after backgroundScanner exists.
|
||||||
directoryChangesEvents?.Dispose();
|
directoryChangesEvents?.Dispose();
|
||||||
|
|
||||||
lock (fsCache)
|
lock (fsCacheLocker)
|
||||||
fsCache.Clear();
|
fsCache.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ namespace FileManager
|
|||||||
{
|
{
|
||||||
while (directoryChangesEvents.TryTake(out FileSystemEventArgs change, -1))
|
while (directoryChangesEvents.TryTake(out FileSystemEventArgs change, -1))
|
||||||
{
|
{
|
||||||
lock (fsCache)
|
lock (fsCacheLocker)
|
||||||
UpdateLocalCache(change);
|
UpdateLocalCache(change);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,9 +146,7 @@ namespace FileManager
|
|||||||
private void AddUniqueFiles(IEnumerable<string> newFiles)
|
private void AddUniqueFiles(IEnumerable<string> newFiles)
|
||||||
{
|
{
|
||||||
foreach (var file in newFiles)
|
foreach (var file in newFiles)
|
||||||
{
|
|
||||||
AddUniqueFile(file);
|
AddUniqueFile(file);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private void AddUniqueFile(string newFile)
|
private void AddUniqueFile(string newFile)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -43,11 +43,12 @@ namespace FileManager
|
|||||||
public static event EventHandler<PictureCachedEventArgs> PictureCached;
|
public static event EventHandler<PictureCachedEventArgs> PictureCached;
|
||||||
|
|
||||||
private static BlockingCollection<PictureDefinition> DownloadQueue { get; } = new BlockingCollection<PictureDefinition>();
|
private static BlockingCollection<PictureDefinition> DownloadQueue { get; } = new BlockingCollection<PictureDefinition>();
|
||||||
|
private static object cacheLocker { get; } = new object();
|
||||||
private static Dictionary<PictureDefinition, byte[]> cache { get; } = new Dictionary<PictureDefinition, byte[]>();
|
private static Dictionary<PictureDefinition, byte[]> cache { get; } = new Dictionary<PictureDefinition, byte[]>();
|
||||||
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
|
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
|
||||||
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
|
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
|
||||||
{
|
{
|
||||||
lock (cache)
|
lock (cacheLocker)
|
||||||
{
|
{
|
||||||
if (cache.ContainsKey(def))
|
if (cache.ContainsKey(def))
|
||||||
return (false, cache[def]);
|
return (false, cache[def]);
|
||||||
@ -67,7 +68,7 @@ namespace FileManager
|
|||||||
|
|
||||||
public static byte[] GetPictureSynchronously(PictureDefinition def)
|
public static byte[] GetPictureSynchronously(PictureDefinition def)
|
||||||
{
|
{
|
||||||
lock (cache)
|
lock (cacheLocker)
|
||||||
{
|
{
|
||||||
if (!cache.ContainsKey(def) || cache[def] == null)
|
if (!cache.ContainsKey(def) || cache[def] == null)
|
||||||
{
|
{
|
||||||
@ -104,7 +105,7 @@ namespace FileManager
|
|||||||
|
|
||||||
var bytes = downloadBytes(def);
|
var bytes = downloadBytes(def);
|
||||||
saveFile(def, bytes);
|
saveFile(def, bytes);
|
||||||
lock (cache)
|
lock (cacheLocker)
|
||||||
cache[def] = bytes;
|
cache[def] = bytes;
|
||||||
|
|
||||||
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });
|
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user