From 55b45ae45d7db2e88f67ef2d302e21074bdadc34 Mon Sep 17 00:00:00 2001 From: rmcrackan Date: Fri, 15 May 2026 12:02:23 -0400 Subject: [PATCH] #1811 - fix UI threading --- Source/LibationAvalonia/ViewModels/MainVM.Import.cs | 5 +++-- Source/LibationWinForms/Form1.ScanNotification.cs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Source/LibationAvalonia/ViewModels/MainVM.Import.cs b/Source/LibationAvalonia/ViewModels/MainVM.Import.cs index d8f2af1f..9b68385d 100644 --- a/Source/LibationAvalonia/ViewModels/MainVM.Import.cs +++ b/Source/LibationAvalonia/ViewModels/MainVM.Import.cs @@ -2,6 +2,7 @@ using ApplicationServices; using AudibleUtilities; using Avalonia.Controls; using Avalonia.Input; +using Avalonia.Threading; using LibationFileManager; using LibationUiBase; using LibationUiBase.Forms; @@ -71,8 +72,8 @@ public partial class MainVM AutoScanChecked = Configuration.Instance.AutoScan; setyNumScanningAccounts(0); - LibraryCommands.ScanBegin += (_, accountsLength) => setyNumScanningAccounts(accountsLength); - LibraryCommands.ScanEnd += (_, newCount) => setyNumScanningAccounts(0); + LibraryCommands.ScanBegin += (_, accountsLength) => Dispatcher.UIThread.Post(() => setyNumScanningAccounts(accountsLength)); + LibraryCommands.ScanEnd += (_, _) => Dispatcher.UIThread.Post(() => setyNumScanningAccounts(0)); if (!Design.IsDesignMode) RemoveButtonsVisible = false; diff --git a/Source/LibationWinForms/Form1.ScanNotification.cs b/Source/LibationWinForms/Form1.ScanNotification.cs index 004e597b..a235365b 100644 --- a/Source/LibationWinForms/Form1.ScanNotification.cs +++ b/Source/LibationWinForms/Form1.ScanNotification.cs @@ -13,6 +13,13 @@ public partial class Form1 private void LibraryCommands_ScanBegin(object? sender, int accountsLength) { + // ImportAccountAsync (incl. auto-scan) runs on a worker thread; ToolStrip must be touched on the UI thread. + if (InvokeRequired) + { + BeginInvoke(LibraryCommands_ScanBegin, sender, accountsLength); + return; + } + removeLibraryBooksToolStripMenuItem.Enabled = false; removeAllAccountsToolStripMenuItem.Enabled = false; removeSomeAccountsToolStripMenuItem.Enabled = false; @@ -30,6 +37,12 @@ public partial class Form1 private void LibraryCommands_ScanEnd(object? sender, int newCount) { + if (InvokeRequired) + { + BeginInvoke(LibraryCommands_ScanEnd, sender, newCount); + return; + } + removeLibraryBooksToolStripMenuItem.Enabled = true; removeAllAccountsToolStripMenuItem.Enabled = true; removeSomeAccountsToolStripMenuItem.Enabled = true;