Improved PictureStorage thread safety and more intuitive naming.
This commit is contained in:
parent
7a90d9fba9
commit
27ae5facbe
@ -34,12 +34,10 @@ namespace FileManager
|
||||
private static string getPath(PictureDefinition def)
|
||||
=> Path.Combine(ImagesDirectory, $"{def.PictureId}{def.Size}.jpg");
|
||||
|
||||
static Task backgroundDownloader;
|
||||
|
||||
static PictureStorage()
|
||||
{
|
||||
backgroundDownloader = new Task(BackgroundDownloader);
|
||||
backgroundDownloader.Start();
|
||||
new Task(BackgroundDownloader, TaskCreationOptions.LongRunning)
|
||||
.Start();
|
||||
}
|
||||
|
||||
public static event EventHandler<PictureCachedEventArgs> PictureCached;
|
||||
@ -49,39 +47,45 @@ namespace FileManager
|
||||
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
|
||||
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
|
||||
{
|
||||
if (cache.ContainsKey(def))
|
||||
return (false, cache[def]);
|
||||
|
||||
var path = getPath(def);
|
||||
|
||||
if (File.Exists(path))
|
||||
lock (cache)
|
||||
{
|
||||
cache[def] = File.ReadAllBytes(path);
|
||||
return (false, cache[def]);
|
||||
}
|
||||
if (cache.ContainsKey(def))
|
||||
return (false, cache[def]);
|
||||
|
||||
DownloadQueue.Add(def);
|
||||
return (true, getDefaultImage(def.Size));
|
||||
var path = getPath(def);
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
cache[def] = File.ReadAllBytes(path);
|
||||
return (false, cache[def]);
|
||||
}
|
||||
|
||||
DownloadQueue.Add(def);
|
||||
return (true, getDefaultImage(def.Size));
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] GetPictureSynchronously(PictureDefinition def)
|
||||
{
|
||||
if (!cache.ContainsKey(def) || cache[def] == null)
|
||||
lock (cache)
|
||||
{
|
||||
var path = getPath(def);
|
||||
byte[] bytes;
|
||||
|
||||
if (File.Exists(path))
|
||||
bytes = File.ReadAllBytes(path);
|
||||
else
|
||||
if (!cache.ContainsKey(def) || cache[def] == null)
|
||||
{
|
||||
bytes = downloadBytes(def);
|
||||
saveFile(def, bytes);
|
||||
}
|
||||
var path = getPath(def);
|
||||
byte[] bytes;
|
||||
|
||||
cache[def] = bytes;
|
||||
if (File.Exists(path))
|
||||
bytes = File.ReadAllBytes(path);
|
||||
else
|
||||
{
|
||||
bytes = downloadBytes(def);
|
||||
saveFile(def, bytes);
|
||||
}
|
||||
|
||||
cache[def] = bytes;
|
||||
}
|
||||
return cache[def];
|
||||
}
|
||||
return cache[def];
|
||||
}
|
||||
|
||||
public static void SetDefaultImage(PictureSize pictureSize, byte[] bytes)
|
||||
@ -100,7 +104,8 @@ namespace FileManager
|
||||
|
||||
var bytes = downloadBytes(def);
|
||||
saveFile(def, bytes);
|
||||
cache[def] = bytes;
|
||||
lock (cache)
|
||||
cache[def] = bytes;
|
||||
|
||||
PictureCached?.Invoke(nameof(PictureStorage), new PictureCachedEventArgs { Definition = def, Picture = bytes });
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ using System.Linq;
|
||||
|
||||
namespace LibationWinForms
|
||||
{
|
||||
internal class GridEntry : AsyncNotifyPropertyChanged, IObjectMemberComparable
|
||||
internal class GridEntry : AsyncNotifyPropertyChanged, IMemberComparable
|
||||
{
|
||||
#region implementation properties
|
||||
// hide from public fields from Data Source GUI with [Browsable(false)]
|
||||
|
||||
@ -3,7 +3,7 @@ using System.Collections;
|
||||
|
||||
namespace LibationWinForms
|
||||
{
|
||||
internal interface IObjectMemberComparable
|
||||
internal interface IMemberComparable
|
||||
{
|
||||
IComparer GetMemberComparer(Type memberType);
|
||||
object GetMemberValue(string memberName);
|
||||
@ -3,7 +3,7 @@ using System.ComponentModel;
|
||||
|
||||
namespace LibationWinForms
|
||||
{
|
||||
internal class ObjectMemberComparer<T> : IComparer<T> where T : IObjectMemberComparable
|
||||
internal class MemberComparer<T> : IComparer<T> where T : IMemberComparable
|
||||
{
|
||||
public ListSortDirection Direction { get; set; } = ListSortDirection.Ascending;
|
||||
public string PropertyName { get; set; }
|
||||
@ -5,7 +5,7 @@ using System.ComponentModel;
|
||||
|
||||
namespace LibationWinForms
|
||||
{
|
||||
internal class SortableBindingList2<T> : BindingList<T> where T : IObjectMemberComparable
|
||||
internal class SortableBindingList2<T> : BindingList<T> where T : IMemberComparable
|
||||
{
|
||||
private bool isSorted;
|
||||
private ListSortDirection listSortDirection;
|
||||
@ -14,7 +14,7 @@ namespace LibationWinForms
|
||||
public SortableBindingList2() : base(new List<T>()) { }
|
||||
public SortableBindingList2(IEnumerable<T> enumeration) : base(new List<T>(enumeration)) { }
|
||||
|
||||
private ObjectMemberComparer<T> Comparer { get; } = new();
|
||||
private MemberComparer<T> Comparer { get; } = new();
|
||||
protected override bool SupportsSortingCore => true;
|
||||
protected override bool SupportsSearchingCore => true;
|
||||
protected override bool IsSortedCore => isSorted;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user