40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System.Collections.Concurrent;
|
|
using RobotNet10.NavigationTune.Interfaces;
|
|
|
|
namespace RobotNet10.NavigationTune.Services;
|
|
|
|
/// <summary>
|
|
/// Singleton registry mapping testRunId to CancellationTokenSource so Stop/EMC Stop can cancel the running test.
|
|
/// </summary>
|
|
public class RunningTestCancellationRegistry : IRunningTestCancellationRegistry
|
|
{
|
|
private readonly ConcurrentDictionary<Guid, CancellationTokenSource> _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) { }
|
|
}
|
|
}
|
|
}
|