Initial commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user