From 5929a31b09df0c8233526538bf3296c67a3dcad8 Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Mon, 26 Feb 2024 20:43:36 -0500 Subject: [PATCH] Fix theme load causing import dialog to freeze --- scripts/package.ps1 | 2 +- src/ThemeDialog.cs | 68 +++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/scripts/package.ps1 b/scripts/package.ps1 index 93084aa..0b5005a 100644 --- a/scripts/package.ps1 +++ b/scripts/package.ps1 @@ -5,7 +5,7 @@ if (-Not (Get-Command msbuild -ErrorAction SilentlyContinue)) { Set-Location "$(Split-Path $MyInvocation.MyCommand.Path)\.." -$appPlatforms = @("x86", "x64", "arm64") +$appPlatforms = if ($args) { $args } else { @("x86", "x64", "arm64") } $appVersion = ([Xml](Get-Content -Path "src\WinDynamicDesktop.csproj")).Project.PropertyGroup.Version Remove-Item "dist" -ErrorAction Ignore -Recurse diff --git a/src/ThemeDialog.cs b/src/ThemeDialog.cs index eac0918..3028e2a 100644 --- a/src/ThemeDialog.cs +++ b/src/ThemeDialog.cs @@ -11,6 +11,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using WindowsDisplayAPI.DisplayConfig; @@ -20,6 +21,7 @@ namespace WinDynamicDesktop public partial class ThemeDialog : Form { private int selectedIndex; + private SemaphoreSlim loadSemaphore = new SemaphoreSlim(1); private static readonly Func _ = Localization.GetTranslation; private const string themeLink = "https://windd.info/themes/"; @@ -135,53 +137,44 @@ private Bitmap GetMeatballIcon() private void LoadThemes(List themes, string activeTheme = null, string focusTheme = null) { + this.loadSemaphore.Wait(60000); Size thumbnailSize = ThemeThumbLoader.GetThumbnailSize(this); ListViewItem focusedItem = null; foreach (ThemeConfig theme in themes.ToList()) { - ListViewItem tempItem = this.Invoke(new Func(() => + try { - string itemText = ThemeManager.GetThemeName(theme); - if (JsonConfig.settings.favoriteThemes != null && - JsonConfig.settings.favoriteThemes.Contains(theme.themeId)) + using (Image thumbnailImage = ThemeThumbLoader.GetThumbnailImage(theme, thumbnailSize, true)) { - itemText = "★ " + itemText; - } - ListViewItem newItem = listView1.Items.Add(itemText); - newItem.Tag = theme.themeId; - - if (activeTheme != null && activeTheme == theme.themeId) - { - newItem.Font = new Font(newItem.Font, FontStyle.Bold); - } - if (focusTheme == null || focusTheme == theme.themeId) - { - focusedItem = newItem; - } - - return newItem; - })); - - this.BeginInvoke(new Action((newItem) => - { - try - { - using (Image thumbnailImage = ThemeThumbLoader.GetThumbnailImage(theme, thumbnailSize, true)) + this.Invoke(new Action(() => { listView1.LargeImageList.Images.Add(thumbnailImage); - newItem.ImageIndex = listView1.LargeImageList.Images.Count - 1; - } + string itemText = ThemeManager.GetThemeName(theme); + if (JsonConfig.settings.favoriteThemes != null && + JsonConfig.settings.favoriteThemes.Contains(theme.themeId)) + { + itemText = "★ " + itemText; + } + ListViewItem newItem = listView1.Items.Add(itemText, + listView1.LargeImageList.Images.Count - 1); + newItem.Tag = theme.themeId; + + if (activeTheme != null && activeTheme == theme.themeId) + { + newItem.Font = new Font(newItem.Font, FontStyle.Bold); + } + if (focusTheme == null || focusTheme == theme.themeId) + { + focusedItem = newItem; + } + })); } - catch (OutOfMemoryException) - { - ThemeLoader.HandleError(new FailedToCreateThumbnail(theme.themeId)); - if (ThemeManager.themeSettings.Find(t => t.themeId == theme.themeId) == null) - { - listView1.Items.RemoveAt(newItem.Index); - } - } - }), tempItem); + } + catch (OutOfMemoryException) + { + ThemeLoader.HandleError(new FailedToCreateThumbnail(theme.themeId)); + } } this.Invoke(new Action(() => @@ -198,6 +191,7 @@ private void LoadThemes(List themes, string activeTheme = null, str ThemeThumbLoader.CacheThumbnails(listView1); })); + this.loadSemaphore.Release(); } private void LoadImportedThemes(List themes, ImportDialog importDialog)