/// <summary>
/// Remove the <see cref="ScriptHost"/> instance from the live instances collection,
/// allowing it to finish currently executing functions before stopping and disposing of it.
/// </summary>
/// <param name="instance">The <see cref="ScriptHost"/> instance to remove</param>
/// <param name="forceStop">Forces the call to stop and dispose of the instance, even if it isn't present in the live instances collection.</param>
private async Task Orphan(ScriptHost instance, bool forceStop = false)
{
lock (_liveInstances)
{
bool removed = _liveInstances.Remove(instance);
if (!forceStop && !removed)
{
return; // somebody else is handling it
}
}
try
{
// this thread now owns the instance
if (instance.TraceWriter != null)
{
instance.TraceWriter.Info("Stopping Host");
}
await instance.StopAsync();
}
finally
{
instance.Dispose();
}
}