fix: marshal timer callbacks to UI thread to prevent GDI+ threading exception
System.Timers.Timer fires on thread pool threads, causing GraphicsPath objects to be accessed concurrently by hover detection and OnPaint, triggering "Object is currently in use elsewhere" in DrawParts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -621,15 +621,21 @@ namespace OpenNest.Controls
|
|||||||
|
|
||||||
private void redrawTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
private void redrawTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
||||||
{
|
{
|
||||||
Invalidate();
|
if (IsDisposed || !IsHandleCreated) return;
|
||||||
|
BeginInvoke(new System.Action(Invalidate));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void hoverTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
private void hoverTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
if (IsDisposed || !IsHandleCreated) return;
|
||||||
|
BeginInvoke(new System.Action(HoverCheck));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HoverCheck()
|
||||||
{
|
{
|
||||||
var graphPt = PointControlToGraph(hoverPoint);
|
var graphPt = PointControlToGraph(hoverPoint);
|
||||||
LayoutPart hitPart = null;
|
LayoutPart hitPart = null;
|
||||||
try
|
|
||||||
{
|
|
||||||
for (var i = parts.Count - 1; i >= 0; --i)
|
for (var i = parts.Count - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (parts[i].Path.GetBounds().Contains(graphPt) &&
|
if (parts[i].Path.GetBounds().Contains(graphPt) &&
|
||||||
@@ -639,12 +645,6 @@ namespace OpenNest.Controls
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
// GraphicsPath in use by paint thread — skip this hover tick
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
hoveredPart = hitPart;
|
hoveredPart = hitPart;
|
||||||
showTooltip = hitPart != null;
|
showTooltip = hitPart != null;
|
||||||
|
|||||||
Reference in New Issue
Block a user