internal void Update() { // Clear out any processes which have ended. _trackedProcesses = _trackedProcesses.Where(tp => !tp.HasExited).ToList(); // CAUTION: This code is subject to a race condition where between one // call to update and the next, all the processes in the list ended, but new // processes (which we are not yet tracking) were created. In that case, // _trackedProcesses would now be empty, and the ProcessWatchdog would exit, // even though there are still processes we care about. // // This should not happen for the scenarios we care about, since the parent // process should outlive all its descendants. if (_trackedProcesses.Any()) { // Add any new descendants of the remaining processes (that is, any // descendants that we're not already tracking). UniqueProcess[] existingProcesses = _trackedProcesses.Select(tp => tp.Process).ToArray(); foreach (Process descendant in GetDescendants(_parentProcess.Id)) { UniqueProcess uniqueDescendant; if (UniqueProcess.TryCreate(descendant, out uniqueDescendant)) { if (!existingProcesses.Contains(uniqueDescendant)) { TrackProcess(descendant); } } } } }
internal static bool TryCreate( Process process, Process procDumpProcess, string description, out TrackedProcess trackedProcess) { bool result = false; trackedProcess = null; if (UniqueProcess.TryCreate(process, out var uniqueProcess) && UniqueProcess.TryCreate(procDumpProcess, out var uniqueProcDumpProcess)) { trackedProcess = new TrackedProcess(uniqueProcess, uniqueProcDumpProcess, description); result = true; } return(result); }