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>
This commit is contained in:
copilot-swe-agent[bot]
2025-10-16 20:13:01 +00:00
parent cfb707d20f
commit 6f52b82a6d
2 changed files with 39 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ namespace mRemoteNG.UI.Forms
private string _pageName;
private readonly DisplayProperties _display = new();
private readonly List<string> _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;
}

View File

@@ -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));
}
}
}