using System.Collections.Concurrent; using RobotNet10.NavigationTune.Interfaces; namespace RobotNet10.NavigationTune.Services; /// /// Singleton registry mapping testRunId to CancellationTokenSource so Stop/EMC Stop can cancel the running test. /// public class RunningTestCancellationRegistry : IRunningTestCancellationRegistry { private readonly ConcurrentDictionary _map = new(); public void Register(Guid testRunId, CancellationTokenSource cts) { _map[testRunId] = cts; } public bool TryCancel(Guid testRunId) { if (_map.TryRemove(testRunId, out var cts)) { try { cts.Cancel(); return true; } catch (ObjectDisposedException) { return false; } } return false; } public void Unregister(Guid testRunId) { if (_map.TryRemove(testRunId, out var cts)) { try { cts.Dispose(); } catch (ObjectDisposedException) { } } } }