/// <summary>
/// Stops the specified primary worker
/// </summary>
/// <param name="worker">The worker.</param>
public void StopPrimary(IPrimaryWorker worker)
{
Guard.NotNull(() => worker, worker);
_waitForEventOrCancel.Set();
_waitForEventOrCancel.Cancel();
//stop is a blocking operation for the primary worker
Task.Run(worker.Stop);
//wait for workers to stop
WaitForDelegate.Wait(() => worker.Running, _configuration.TimeToWaitForWorkersToStop);
if (worker.Running)
{
worker.AttemptToTerminate();
}
//attempt to cancel workers if any are still running
if (worker.Running)
{
_cancelWorkSource.CancellationTokenSource.Cancel();
WaitForDelegate.Wait(() => worker.Running, _configuration.TimeToWaitForWorkersToCancel);
if (worker.Running)
{
worker.AttemptToTerminate();
}
}
//force kill workers that are still running by aborting the thread, or waiting until work has completed
if (worker.Running)
{
worker.TryForceTerminate();
}
}