@@ -19,14 +19,14 @@ public static class ImageLoader
1919 private static readonly string ClassName = nameof ( ImageLoader ) ;
2020
2121 private static readonly ImageCache ImageCache = new ( ) ;
22- private static SemaphoreSlim storageLock { get ; } = new SemaphoreSlim ( 1 , 1 ) ;
22+ private static Lock storageLock { get ; } = new ( ) ;
2323 private static BinaryStorage < List < ( string , bool ) > > _storage ;
2424 private static readonly ConcurrentDictionary < string , string > GuidToKey = new ( ) ;
2525 private static IImageHashGenerator _hashGenerator ;
2626 private static readonly bool EnableImageHash = true ;
27- public static ImageSource Image { get ; } = new BitmapImage ( new Uri ( Constant . ImageIcon ) ) ;
28- public static ImageSource MissingImage { get ; } = new BitmapImage ( new Uri ( Constant . MissingImgIcon ) ) ;
29- public static ImageSource LoadingImage { get ; } = new BitmapImage ( new Uri ( Constant . LoadingImgIcon ) ) ;
27+ public static ImageSource Image => ImageCache [ Constant . ImageIcon , false ] ;
28+ public static ImageSource MissingImage => ImageCache [ Constant . MissingImgIcon , false ] ;
29+ public static ImageSource LoadingImage => ImageCache [ Constant . LoadingImgIcon , false ] ;
3030 public const int SmallIconSize = 64 ;
3131 public const int FullIconSize = 256 ;
3232 public const int FullImageSize = 320 ;
@@ -36,20 +36,25 @@ public static class ImageLoader
3636
3737 public static async Task InitializeAsync ( )
3838 {
39- _storage = new BinaryStorage < List < ( string , bool ) > > ( "Image" ) ;
40- _hashGenerator = new ImageHashGenerator ( ) ;
39+ var usage = await Task . Run ( ( ) =>
40+ {
41+ _storage = new BinaryStorage < List < ( string , bool ) > > ( "Image" ) ;
42+ _hashGenerator = new ImageHashGenerator ( ) ;
4143
42- var usage = await LoadStorageToConcurrentDictionaryAsync ( ) ;
43- _storage . ClearData ( ) ;
44+ var usage = LoadStorageToConcurrentDictionary ( ) ;
45+ _storage . ClearData ( ) ;
4446
45- ImageCache . Initialize ( usage ) ;
47+ ImageCache . Initialize ( usage ) ;
4648
47- foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . MissingImgIcon } )
48- {
49- ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
50- img . Freeze ( ) ;
51- ImageCache [ icon , false ] = img ;
52- }
49+ foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . ImageIcon , Constant . MissingImgIcon , Constant . LoadingImgIcon } )
50+ {
51+ ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
52+ img . Freeze ( ) ;
53+ ImageCache [ icon , false ] = img ;
54+ }
55+
56+ return usage ;
57+ } ) ;
5358
5459 _ = Task . Run ( async ( ) =>
5560 {
@@ -64,42 +69,26 @@ await Stopwatch.InfoAsync(ClassName, "Preload images cost", async () =>
6469 } ) ;
6570 }
6671
67- public static async Task SaveAsync ( )
72+ public static void Save ( )
6873 {
69- await storageLock . WaitAsync ( ) ;
70-
71- try
72- {
73- await _storage . SaveAsync ( ImageCache . EnumerateEntries ( )
74- . Select ( x => x . Key )
75- . ToList ( ) ) ;
76- }
77- catch ( System . Exception e )
74+ lock ( storageLock )
7875 {
79- Log . Exception ( ClassName , "Failed to save image cache to file" , e ) ;
80- }
81- finally
82- {
83- storageLock . Release ( ) ;
76+ try
77+ {
78+ _storage . Save ( [ .. ImageCache . EnumerateEntries ( ) . Select ( x => x . Key ) ] ) ;
79+ }
80+ catch ( System . Exception e )
81+ {
82+ Log . Exception ( ClassName , "Failed to save image cache to file" , e ) ;
83+ }
8484 }
8585 }
8686
87- public static async Task WaitSaveAsync ( )
87+ private static List < ( string , bool ) > LoadStorageToConcurrentDictionary ( )
8888 {
89- await storageLock . WaitAsync ( ) ;
90- storageLock . Release ( ) ;
91- }
92-
93- private static async Task < List < ( string , bool ) > > LoadStorageToConcurrentDictionaryAsync ( )
94- {
95- await storageLock . WaitAsync ( ) ;
96- try
97- {
98- return await _storage . TryLoadAsync ( new List < ( string , bool ) > ( ) ) ;
99- }
100- finally
89+ lock ( storageLock )
10190 {
102- storageLock . Release ( ) ;
91+ return _storage . TryLoad ( [ ] ) ;
10392 }
10493 }
10594
@@ -174,7 +163,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
174163 Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on first try", e ) ;
175164 Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on second try", e2 ) ;
176165
177- ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
166+ ImageSource image = MissingImage ;
178167 ImageCache [ path , false ] = image ;
179168 imageResult = new ImageResult ( image , ImageType . Error ) ;
180169 }
@@ -273,7 +262,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
273262 }
274263 else
275264 {
276- image = ImageCache [ Constant . MissingImgIcon , false ] ;
265+ image = MissingImage ;
277266 path = Constant . MissingImgIcon ;
278267 }
279268
0 commit comments