fix(engine): throttle progress reports to 150ms intervals
Parallel loops were flooding the UI with per-angle/per-candidate reports faster than WinForms could render them. Use Interlocked timestamp checks to report at most every 150ms, keeping descriptions readable. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -285,6 +285,8 @@ namespace OpenNest
|
|||||||
var linearBag = new System.Collections.Concurrent.ConcurrentBag<(FillScore score, List<Part> parts)>();
|
var linearBag = new System.Collections.Concurrent.ConcurrentBag<(FillScore score, List<Part> parts)>();
|
||||||
var angleBag = new System.Collections.Concurrent.ConcurrentBag<AngleResult>();
|
var angleBag = new System.Collections.Concurrent.ConcurrentBag<AngleResult>();
|
||||||
|
|
||||||
|
long lastLinearReport = 0;
|
||||||
|
|
||||||
System.Threading.Tasks.Parallel.ForEach(angles,
|
System.Threading.Tasks.Parallel.ForEach(angles,
|
||||||
new System.Threading.Tasks.ParallelOptions { CancellationToken = token },
|
new System.Threading.Tasks.ParallelOptions { CancellationToken = token },
|
||||||
angle =>
|
angle =>
|
||||||
@@ -305,14 +307,19 @@ namespace OpenNest
|
|||||||
angleBag.Add(new AngleResult { AngleDeg = angleDeg, Direction = NestDirection.Vertical, PartCount = v.Count });
|
angleBag.Add(new AngleResult { AngleDeg = angleDeg, Direction = NestDirection.Vertical, PartCount = v.Count });
|
||||||
}
|
}
|
||||||
|
|
||||||
var bestDir = (h?.Count ?? 0) >= (v?.Count ?? 0) ? "H" : "V";
|
var now = linearSw.ElapsedMilliseconds;
|
||||||
var bestCount = System.Math.Max(h?.Count ?? 0, v?.Count ?? 0);
|
if (progress != null && now - Interlocked.Read(ref lastLinearReport) >= 150)
|
||||||
progress?.Report(new NestProgress
|
|
||||||
{
|
{
|
||||||
Phase = NestPhase.Linear,
|
Interlocked.Exchange(ref lastLinearReport, now);
|
||||||
PlateNumber = PlateNumber,
|
var bestDir = (h?.Count ?? 0) >= (v?.Count ?? 0) ? "H" : "V";
|
||||||
Description = $"Linear: {angleDeg:F0}° {bestDir} - {bestCount} parts"
|
var bestCount = System.Math.Max(h?.Count ?? 0, v?.Count ?? 0);
|
||||||
});
|
progress.Report(new NestProgress
|
||||||
|
{
|
||||||
|
Phase = NestPhase.Linear,
|
||||||
|
PlateNumber = PlateNumber,
|
||||||
|
Description = $"Linear: {angleDeg:F0}° {bestDir} - {bestCount} parts"
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
linearSw.Stop();
|
linearSw.Stop();
|
||||||
AngleResults.AddRange(angleBag);
|
AngleResults.AddRange(angleBag);
|
||||||
@@ -515,6 +522,9 @@ namespace OpenNest
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var pairsSw = Stopwatch.StartNew();
|
||||||
|
long lastPairsReport = 0;
|
||||||
|
|
||||||
System.Threading.Tasks.Parallel.For(0, candidates.Count,
|
System.Threading.Tasks.Parallel.For(0, candidates.Count,
|
||||||
new System.Threading.Tasks.ParallelOptions { CancellationToken = token },
|
new System.Threading.Tasks.ParallelOptions { CancellationToken = token },
|
||||||
i =>
|
i =>
|
||||||
@@ -528,12 +538,17 @@ namespace OpenNest
|
|||||||
if (filled != null && filled.Count > 0)
|
if (filled != null && filled.Count > 0)
|
||||||
resultBag.Add((FillScore.Compute(filled, workArea), filled));
|
resultBag.Add((FillScore.Compute(filled, workArea), filled));
|
||||||
|
|
||||||
progress?.Report(new NestProgress
|
var now = pairsSw.ElapsedMilliseconds;
|
||||||
|
if (progress != null && now - Interlocked.Read(ref lastPairsReport) >= 150)
|
||||||
{
|
{
|
||||||
Phase = NestPhase.Pairs,
|
Interlocked.Exchange(ref lastPairsReport, now);
|
||||||
PlateNumber = PlateNumber,
|
progress.Report(new NestProgress
|
||||||
Description = $"Pairs: candidate {i + 1}/{candidates.Count} - {filled?.Count ?? 0} parts"
|
{
|
||||||
});
|
Phase = NestPhase.Pairs,
|
||||||
|
PlateNumber = PlateNumber,
|
||||||
|
Description = $"Pairs: candidate {i + 1}/{candidates.Count} - {filled?.Count ?? 0} parts"
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
|
|||||||
Reference in New Issue
Block a user