@@ -17,14 +17,14 @@ public static class ImageLoader
17
17
private static readonly string ClassName = nameof ( ImageLoader ) ;
18
18
19
19
private static readonly ImageCache ImageCache = new ( ) ;
20
- private static SemaphoreSlim storageLock { get ; } = new SemaphoreSlim ( 1 , 1 ) ;
20
+ private static Lock storageLock { get ; } = new ( ) ;
21
21
private static BinaryStorage < List < ( string , bool ) > > _storage ;
22
22
private static readonly ConcurrentDictionary < string , string > GuidToKey = new ( ) ;
23
23
private static IImageHashGenerator _hashGenerator ;
24
24
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 ] ;
28
28
public const int SmallIconSize = 64 ;
29
29
public const int FullIconSize = 256 ;
30
30
public const int FullImageSize = 320 ;
@@ -34,31 +34,29 @@ public static class ImageLoader
34
34
35
35
public static async Task InitializeAsync ( )
36
36
{
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 ( ) ;
39
41
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 ( ) ;
43
45
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
+ } ) ;
50
53
}
51
54
52
- private static async Task < List < ( string , bool ) > > LoadStorageToConcurrentDictionaryAsync ( )
55
+ private static List < ( string , bool ) > LoadStorageToConcurrentDictionary ( )
53
56
{
54
- await storageLock . WaitAsync ( ) ;
55
- try
56
- {
57
- return await _storage . TryLoadAsync ( new List < ( string , bool ) > ( ) ) ;
58
- }
59
- finally
57
+ lock ( storageLock )
60
58
{
61
- storageLock . Release ( ) ;
59
+ return _storage . TryLoad ( [ ] ) ;
62
60
}
63
61
}
64
62
@@ -109,7 +107,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
109
107
{
110
108
Log . Error ( ClassName , $ "Failed to load image from path { path } : Remote images are not supported.") ;
111
109
112
- ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
110
+ ImageSource image = MissingImage ;
113
111
ImageCache [ path , false ] = image ;
114
112
imageResult = new ImageResult ( image , ImageType . Error ) ;
115
113
}
@@ -137,7 +135,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
137
135
Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on first try", e ) ;
138
136
Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on second try", e2 ) ;
139
137
140
- ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
138
+ ImageSource image = MissingImage ;
141
139
ImageCache [ path , false ] = image ;
142
140
imageResult = new ImageResult ( image , ImageType . Error ) ;
143
141
}
@@ -205,7 +203,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
205
203
}
206
204
else
207
205
{
208
- image = ImageCache [ Constant . MissingImgIcon , false ] ;
206
+ image = MissingImage ;
209
207
path = Constant . MissingImgIcon ;
210
208
}
211
209
0 commit comments