1111using System . IO ;
1212using System . Linq ;
1313using System . Runtime . InteropServices ;
14+ using System . Threading ;
1415using System . Threading . Tasks ;
1516using System . Windows . Forms ;
1617using WindowsDisplayAPI . DisplayConfig ;
@@ -20,6 +21,7 @@ namespace WinDynamicDesktop
2021 public partial class ThemeDialog : Form
2122 {
2223 private int selectedIndex ;
24+ private SemaphoreSlim loadSemaphore = new SemaphoreSlim ( 1 ) ;
2325
2426 private static readonly Func < string , string > _ = Localization . GetTranslation ;
2527 private const string themeLink = "https://windd.info/themes/" ;
@@ -135,53 +137,44 @@ private Bitmap GetMeatballIcon()
135137
136138 private void LoadThemes ( List < ThemeConfig > themes , string activeTheme = null , string focusTheme = null )
137139 {
140+ this . loadSemaphore . Wait ( 60000 ) ;
138141 Size thumbnailSize = ThemeThumbLoader . GetThumbnailSize ( this ) ;
139142 ListViewItem focusedItem = null ;
140143
141144 foreach ( ThemeConfig theme in themes . ToList ( ) )
142145 {
143- ListViewItem tempItem = this . Invoke ( new Func < ListViewItem > ( ( ) =>
146+ try
144147 {
145- string itemText = ThemeManager . GetThemeName ( theme ) ;
146- if ( JsonConfig . settings . favoriteThemes != null &&
147- JsonConfig . settings . favoriteThemes . Contains ( theme . themeId ) )
148+ using ( Image thumbnailImage = ThemeThumbLoader . GetThumbnailImage ( theme , thumbnailSize , true ) )
148149 {
149- itemText = "★ " + itemText ;
150- }
151- ListViewItem newItem = listView1 . Items . Add ( itemText ) ;
152- newItem . Tag = theme . themeId ;
153-
154- if ( activeTheme != null && activeTheme == theme . themeId )
155- {
156- newItem . Font = new Font ( newItem . Font , FontStyle . Bold ) ;
157- }
158- if ( focusTheme == null || focusTheme == theme . themeId )
159- {
160- focusedItem = newItem ;
161- }
162-
163- return newItem ;
164- } ) ) ;
165-
166- this . BeginInvoke ( new Action < ListViewItem > ( ( newItem ) =>
167- {
168- try
169- {
170- using ( Image thumbnailImage = ThemeThumbLoader . GetThumbnailImage ( theme , thumbnailSize , true ) )
150+ this . Invoke ( new Action ( ( ) =>
171151 {
172152 listView1 . LargeImageList . Images . Add ( thumbnailImage ) ;
173- newItem . ImageIndex = listView1 . LargeImageList . Images . Count - 1 ;
174- }
153+ string itemText = ThemeManager . GetThemeName ( theme ) ;
154+ if ( JsonConfig . settings . favoriteThemes != null &&
155+ JsonConfig . settings . favoriteThemes . Contains ( theme . themeId ) )
156+ {
157+ itemText = "★ " + itemText ;
158+ }
159+ ListViewItem newItem = listView1 . Items . Add ( itemText ,
160+ listView1 . LargeImageList . Images . Count - 1 ) ;
161+ newItem . Tag = theme . themeId ;
162+
163+ if ( activeTheme != null && activeTheme == theme . themeId )
164+ {
165+ newItem . Font = new Font ( newItem . Font , FontStyle . Bold ) ;
166+ }
167+ if ( focusTheme == null || focusTheme == theme . themeId )
168+ {
169+ focusedItem = newItem ;
170+ }
171+ } ) ) ;
175172 }
176- catch ( OutOfMemoryException )
177- {
178- ThemeLoader . HandleError ( new FailedToCreateThumbnail ( theme . themeId ) ) ;
179- if ( ThemeManager . themeSettings . Find ( t => t . themeId == theme . themeId ) == null )
180- {
181- listView1 . Items . RemoveAt ( newItem . Index ) ;
182- }
183- }
184- } ) , tempItem ) ;
173+ }
174+ catch ( OutOfMemoryException )
175+ {
176+ ThemeLoader . HandleError ( new FailedToCreateThumbnail ( theme . themeId ) ) ;
177+ }
185178 }
186179
187180 this . Invoke ( new Action ( ( ) =>
@@ -198,6 +191,7 @@ private void LoadThemes(List<ThemeConfig> themes, string activeTheme = null, str
198191
199192 ThemeThumbLoader . CacheThumbnails ( listView1 ) ;
200193 } ) ) ;
194+ this . loadSemaphore . Release ( ) ;
201195 }
202196
203197 private void LoadImportedThemes ( List < ThemeConfig > themes , ImportDialog importDialog )
0 commit comments