/// <summary> /// Removes all value entries whose corresponding Runspace has been destroyed /// </summary> public void PurgeExpired() { // Store IDs first, so parallel access is not an issue and a new value gets accidentally discarded Guid[] IDs = Values.Keys.ToArray(); ICollection <System.Management.Automation.Runspaces.Runspace> runspaces = UtilityHost.GetRunspaces(); ICollection <Guid> runspaceIDs = (ICollection <Guid>)runspaces.Select(o => o.InstanceId); foreach (Guid ID in IDs) { if (!runspaceIDs.Contains(ID)) { Values.Remove(ID); } } }