dedicated lock objects for safety

This commit is contained in:
Robert McRackan 2021-09-24 11:46:37 -04:00
parent d2eaf26117
commit fd56017af5
3 changed files with 13 additions and 13 deletions

View File

@ -36,6 +36,7 @@ namespace FileManager
}
}
private static object bookDirectoryFilesLocker { get; } = new();
internal static BackgroundFileSystem BookDirectoryFiles { get; set; }
#endregion
@ -66,7 +67,7 @@ namespace FileManager
//If user changed the BooksDirectory, reinitialize.
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
{
lock (BookDirectoryFiles)
lock (bookDirectoryFilesLocker)
{
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
{

View File

@ -25,6 +25,8 @@ namespace FileManager
private FileSystemWatcher fileSystemWatcher { get; set; }
private BlockingCollection<FileSystemEventArgs> directoryChangesEvents { get; set; }
private Task backgroundScanner { get; set; }
private object fsCacheLocker { get; } = new();
private List<string> fsCache { get; } = new();
public BackgroundFileSystem(string rootDirectory, string searchPattern, SearchOption searchOptions)
@ -38,15 +40,13 @@ namespace FileManager
public string FindFile(string regexPattern, RegexOptions options)
{
lock (fsCache)
{
lock (fsCacheLocker)
return fsCache.FirstOrDefault(s => Regex.IsMatch(s, regexPattern, options));
}
}
public void RefreshFiles()
{
lock (fsCache)
lock (fsCacheLocker)
{
fsCache.Clear();
fsCache.AddRange(Directory.EnumerateFiles(RootDirectory, SearchPattern, SearchOption));
@ -57,7 +57,7 @@ namespace FileManager
{
Stop();
lock (fsCache)
lock (fsCacheLocker)
fsCache.AddRange(Directory.EnumerateFiles(RootDirectory, SearchPattern, SearchOption));
directoryChangesEvents = new BlockingCollection<FileSystemEventArgs>();
@ -86,7 +86,7 @@ namespace FileManager
//Dispose of directoryChangesEvents after backgroundScanner exists.
directoryChangesEvents?.Dispose();
lock (fsCache)
lock (fsCacheLocker)
fsCache.Clear();
}
@ -106,7 +106,7 @@ namespace FileManager
{
while (directoryChangesEvents.TryTake(out FileSystemEventArgs change, -1))
{
lock (fsCache)
lock (fsCacheLocker)
UpdateLocalCache(change);
}
}
@ -146,9 +146,7 @@ namespace FileManager
private void AddUniqueFiles(IEnumerable<string> newFiles)
{
foreach (var file in newFiles)
{
AddUniqueFile(file);
}
}
private void AddUniqueFile(string newFile)
{

View File

@ -43,11 +43,12 @@ namespace FileManager
public static event EventHandler<PictureCachedEventArgs> PictureCached;
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<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
{
lock (cache)
lock (cacheLocker)
{
if (cache.ContainsKey(def))
return (false, cache[def]);
@ -67,7 +68,7 @@ namespace FileManager
public static byte[] GetPictureSynchronously(PictureDefinition def)
{
lock (cache)
lock (cacheLocker)
{
if (!cache.ContainsKey(def) || cache[def] == null)
{
@ -104,7 +105,7 @@ namespace FileManager
var bytes = downloadBytes(def);
saveFile(def, bytes);
lock (cache)
lock (cacheLocker)
cache[def] = bytes;
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });