From 6f52b82a6df9e3813ee347ea3bef5943849d4c9e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 16 Oct 2025 20:13:01 +0000 Subject: [PATCH] Fix Options dialog freezing when opening/closing multiple times - Add _isInitialized flag to prevent multiple event subscriptions - Skip re-initialization in FrmOptions_Load when form is reused - Properly clean up Application.Idle handler in FormClosing - Add test to verify form can be shown/hidden multiple times Co-authored-by: Kvarkas <3611964+Kvarkas@users.noreply.github.com> --- mRemoteNG/UI/Forms/frmOptions.cs | 12 +++++++++ mRemoteNGTests/UI/Forms/OptionsFormTests.cs | 27 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/mRemoteNG/UI/Forms/frmOptions.cs b/mRemoteNG/UI/Forms/frmOptions.cs index 267d3b7a..85bd0ed1 100644 --- a/mRemoteNG/UI/Forms/frmOptions.cs +++ b/mRemoteNG/UI/Forms/frmOptions.cs @@ -22,6 +22,7 @@ namespace mRemoteNG.UI.Forms private string _pageName; private readonly DisplayProperties _display = new(); private readonly List _optionPageObjectNames; + private bool _isInitialized = false; public FrmOptions() : this(Language.StartupExit) { @@ -57,6 +58,13 @@ namespace mRemoteNG.UI.Forms private void FrmOptions_Load(object sender, EventArgs e) { + // Only initialize once to prevent multiple event subscriptions and page reloading + if (_isInitialized) + { + this.Visible = true; + return; + } + this.Visible = true; FontOverrider.FontOverride(this); SetActivatedPage(); @@ -71,6 +79,7 @@ namespace mRemoteNG.UI.Forms //ThemeManager.getInstance().ThemeChanged += ApplyTheme; lstOptionPages.SelectedIndexChanged += LstOptionPages_SelectedIndexChanged; lstOptionPages.SelectedIndex = 0; + _isInitialized = true; } private void ApplyTheme() @@ -274,6 +283,9 @@ namespace mRemoteNG.UI.Forms private void FrmOptions_FormClosing(object sender, FormClosingEventArgs e) { + // Ensure Application.Idle handler is removed if still attached + Application.Idle -= Application_Idle; + e.Cancel = true; this.Visible = false; } diff --git a/mRemoteNGTests/UI/Forms/OptionsFormTests.cs b/mRemoteNGTests/UI/Forms/OptionsFormTests.cs index 2b964cb4..947e7c5e 100644 --- a/mRemoteNGTests/UI/Forms/OptionsFormTests.cs +++ b/mRemoteNGTests/UI/Forms/OptionsFormTests.cs @@ -33,5 +33,32 @@ namespace mRemoteNGTests.UI.Forms ListViewTester listViewTester = new("lstOptionPages", _optionsForm); Assert.That(listViewTester.Items.Count, Is.EqualTo(12)); } + + [Test] + public void FormCanBeHiddenAndShownMultipleTimes() + { + // First show (already done in Setup) + Assert.That(_optionsForm.Visible, Is.True); + + // Hide the form + _optionsForm.Hide(); + Assert.That(_optionsForm.Visible, Is.False); + + // Show it again + _optionsForm.Show(); + Assert.That(_optionsForm.Visible, Is.True); + + // Verify pages are still loaded correctly + ListViewTester listViewTester = new("lstOptionPages", _optionsForm); + Assert.That(listViewTester.Items.Count, Is.EqualTo(12)); + + // Hide and show one more time + _optionsForm.Hide(); + _optionsForm.Show(); + Assert.That(_optionsForm.Visible, Is.True); + + // Verify pages are still there + Assert.That(listViewTester.Items.Count, Is.EqualTo(12)); + } } } \ No newline at end of file