From 5620c9b8427f5d7b3ce683ccd55ba7f8649d8976 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 19 Oct 2026 12:05:14 -0400 Subject: [PATCH] fix(ui): validate Nest Info and Edit Plate on the UI thread Both dialogs revalidated with a System.Timers.Timer whose SynchronizingObject was the form. Before the dialog has a handle, InvokeRequired is false, so the Elapsed handler ran EnableCheck on a pool thread. When the UI thread was busy (for example with the cut simulator open), the timer fired before ShowDialog and touched applyButton while the UI thread was creating it, throwing a cross-thread InvalidOperationException. Use a WinForms timer, which always ticks on the UI thread, and stop starting it from the constructor, which already calls EnableCheck directly. Co-Authored-By: Claude Opus 5.5 --- OpenNest/Forms/EditNestInfoForm.cs | 16 +++++++++------- OpenNest/Forms/EditPlateForm.cs | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/OpenNest/Forms/EditNestInfoForm.cs b/OpenNest/Forms/EditNestInfoForm.cs index 40612ae6..bccc8ccd 100644 --- a/OpenNest/Forms/EditNestInfoForm.cs +++ b/OpenNest/Forms/EditNestInfoForm.cs @@ -2,7 +2,7 @@ using System; using System.Windows.Forms; using OpenNest.Data; using OpenNest.Geometry; -using Timer = System.Timers.Timer; +using Timer = System.Windows.Forms.Timer; namespace OpenNest.Forms { @@ -23,14 +23,16 @@ namespace OpenNest.Forms statusBox.Items.Add(status); statusBox.SelectedItem = NestStatus.Quote; - timer = new Timer + // A WinForms timer ticks on the UI thread. System.Timers.Timer's SynchronizingObject + // cannot marshal before the dialog has a handle, so a busy UI thread (e.g. an open + // cut simulator) let it run EnableCheck on a pool thread while the handle was created. + timer = new Timer { Interval = SystemInformation.KeyboardDelay + 1 }; + timer.Tick += (sender, e) => { - SynchronizingObject = this, - Enabled = true, - AutoReset = false, - Interval = SystemInformation.KeyboardDelay + 1, + timer.Stop(); + EnableCheck(); }; - timer.Elapsed += (sender, e) => EnableCheck(); + Disposed += (sender, e) => timer.Dispose(); nameBox.TextChanged += (sender, e) => EnableCheck(); EnableCheck(); } diff --git a/OpenNest/Forms/EditPlateForm.cs b/OpenNest/Forms/EditPlateForm.cs index d2cfda68..e65618d5 100644 --- a/OpenNest/Forms/EditPlateForm.cs +++ b/OpenNest/Forms/EditPlateForm.cs @@ -1,6 +1,6 @@ -using System; +using System; using System.Windows.Forms; -using Timer = System.Timers.Timer; +using Timer = System.Windows.Forms.Timer; namespace OpenNest.Forms { @@ -17,14 +17,16 @@ namespace OpenNest.Forms this.plate = plate; - timer = new Timer + // A WinForms timer ticks on the UI thread. System.Timers.Timer's SynchronizingObject + // cannot marshal before the dialog has a handle, so a busy UI thread (e.g. an open + // cut simulator) let it run EnableCheck on a pool thread while the handle was created. + timer = new Timer { Interval = SystemInformation.KeyboardDelay + 1 }; + timer.Tick += (sender, e) => { - SynchronizingObject = this, - Enabled = true, - AutoReset = false, - Interval = SystemInformation.KeyboardDelay + 1, + timer.Stop(); + EnableCheck(); }; - timer.Elapsed += (sender, e) => EnableCheck(); + Disposed += (sender, e) => timer.Dispose(); Load(plate); EnableCheck();