@@ -17,14 +17,14 @@ public static class ImageLoader
1717 private static readonly string ClassName = nameof ( ImageLoader ) ;
1818
1919 private static readonly ImageCache ImageCache = new ( ) ;
20- private static SemaphoreSlim storageLock { get ; } = new SemaphoreSlim ( 1 , 1 ) ;
20+ private static Lock storageLock { get ; } = new ( ) ;
2121 private static BinaryStorage < List < ( string , bool ) > > _storage ;
2222 private static readonly ConcurrentDictionary < string , string > GuidToKey = new ( ) ;
2323 private static IImageHashGenerator _hashGenerator ;
2424 private static readonly bool EnableImageHash = true ;
25- public static ImageSource Image { get ; } = new BitmapImage ( new Uri ( Constant . ImageIcon ) ) ;
26- public static ImageSource MissingImage { get ; } = new BitmapImage ( new Uri ( Constant . MissingImgIcon ) ) ;
27- public static ImageSource LoadingImage { get ; } = new BitmapImage ( new Uri ( Constant . LoadingImgIcon ) ) ;
25+ public static ImageSource Image => ImageCache [ Constant . ImageIcon , false ] ;
26+ public static ImageSource MissingImage => ImageCache [ Constant . MissingImgIcon , false ] ;
27+ public static ImageSource LoadingImage => ImageCache [ Constant . LoadingImgIcon , false ] ;
2828 public const int SmallIconSize = 64 ;
2929 public const int FullIconSize = 256 ;
3030 public const int FullImageSize = 320 ;
@@ -34,31 +34,29 @@ public static class ImageLoader
3434
3535 public static async Task InitializeAsync ( )
3636 {
37- _storage = new BinaryStorage < List < ( string , bool ) > > ( "Image" ) ;
38- _hashGenerator = new ImageHashGenerator ( ) ;
37+ await Task . Run ( ( ) =>
38+ {
39+ _storage = new BinaryStorage < List < ( string , bool ) > > ( "Image" ) ;
40+ _hashGenerator = new ImageHashGenerator ( ) ;
3941
40- // Even though we no longer do image preloading and thus don't need _storage,
41- // for some reason MemoryPackSerializer exceptions appear when this is removed
42- await LoadStorageToConcurrentDictionaryAsync ( ) ;
42+ // Even though we no longer do image preloading and thus don't need _storage,
43+ // for some reason MemoryPackSerializer exceptions appear when this is removed
44+ LoadStorageToConcurrentDictionary ( ) ;
4345
44- foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . MissingImgIcon } )
45- {
46- ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
47- img . Freeze ( ) ;
48- ImageCache [ icon , false ] = img ;
49- }
46+ foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . ImageIcon , Constant . MissingImgIcon , Constant . LoadingImgIcon } )
47+ {
48+ ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
49+ img . Freeze ( ) ;
50+ ImageCache [ icon , false ] = img ;
51+ }
52+ } ) ;
5053 }
5154
52- private static async Task < List < ( string , bool ) > > LoadStorageToConcurrentDictionaryAsync ( )
55+ private static List < ( string , bool ) > LoadStorageToConcurrentDictionary ( )
5356 {
54- await storageLock . WaitAsync ( ) ;
55- try
56- {
57- return await _storage . TryLoadAsync ( new List < ( string , bool ) > ( ) ) ;
58- }
59- finally
57+ lock ( storageLock )
6058 {
61- storageLock . Release ( ) ;
59+ return _storage . TryLoad ( [ ] ) ;
6260 }
6361 }
6462
@@ -109,7 +107,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
109107 {
110108 Log . Error ( ClassName , $ "Failed to load image from path { path } : Remote images are not supported.") ;
111109
112- ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
110+ ImageSource image = MissingImage ;
113111 ImageCache [ path , false ] = image ;
114112 imageResult = new ImageResult ( image , ImageType . Error ) ;
115113 }
@@ -137,7 +135,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
137135 Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on first try", e ) ;
138136 Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on second try", e2 ) ;
139137
140- ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
138+ ImageSource image = MissingImage ;
141139 ImageCache [ path , false ] = image ;
142140 imageResult = new ImageResult ( image , ImageType . Error ) ;
143141 }
@@ -205,7 +203,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
205203 }
206204 else
207205 {
208- image = ImageCache [ Constant . MissingImgIcon , false ] ;
206+ image = MissingImage ;
209207 path = Constant . MissingImgIcon ;
210208 }
211209
0 commit comments