diff --git a/trunk/ProcessHacker/Components/ProcessTree/ProcessTreeModel.cs b/trunk/ProcessHacker/Components/ProcessTree/ProcessTreeModel.cs index 9fae0e82c..729843bd0 100644 --- a/trunk/ProcessHacker/Components/ProcessTree/ProcessTreeModel.cs +++ b/trunk/ProcessHacker/Components/ProcessTree/ProcessTreeModel.cs @@ -49,49 +49,52 @@ namespace ProcessHacker public void Add(ProcessItem item) { ProcessNode itemNode = new ProcessNode(item); - - // Add the process to the list of all processes. - _processes.Add(item.Pid, itemNode); - - // Find the process' parent and add the process to it if we found it. - if (item.HasParent && _processes.ContainsKey(item.ParentPid)) + + if (!_processes.ContainsKey(item.Pid)) { - ProcessNode parent = _processes[item.ParentPid]; + // Add the process to the list of all processes. + _processes.Add(item.Pid, itemNode); - parent.Children.Add(itemNode); - itemNode.Parent = parent; - } - else - { - // The process doesn't have a parent, so add it to the root nodes. - _roots.Add(itemNode); - } - - itemNode.RefreshTreePath(); - - // Find this process' children and fix them up. - - // We need to create a copy of the array because we may need - // to modify the roots list. - ProcessNode[] roots = _roots.ToArray(); - - foreach (ProcessNode node in roots) - { - // Notice that we don't replace a node's parent if it - // already has one. This is to break potential cyclic - // references. - if (node.Parent == null && node.ProcessItem.HasParent && node.PPid == item.Pid) + // Find the process' parent and add the process to it if we found it. + if (item.HasParent && _processes.ContainsKey(item.ParentPid)) { - // Remove the node from the root list and add it to our - // process' child list. - _roots.Remove(node); - itemNode.Children.Add(node); - node.Parent = itemNode; - node.RefreshTreePathRecursive(); - } - } + ProcessNode parent = _processes[item.ParentPid]; - this.StructureChanged(this, new TreePathEventArgs(new TreePath())); + parent.Children.Add(itemNode); + itemNode.Parent = parent; + } + else + { + // The process doesn't have a parent, so add it to the root nodes. + _roots.Add(itemNode); + } + + itemNode.RefreshTreePath(); + + // Find this process' children and fix them up. + + // We need to create a copy of the array because we may need + // to modify the roots list. + ProcessNode[] roots = _roots.ToArray(); + + foreach (ProcessNode node in roots) + { + // Notice that we don't replace a node's parent if it + // already has one. This is to break potential cyclic + // references. + if (node.Parent == null && node.ProcessItem.HasParent && node.PPid == item.Pid) + { + // Remove the node from the root list and add it to our + // process' child list. + _roots.Remove(node); + itemNode.Children.Add(node); + node.Parent = itemNode; + node.RefreshTreePathRecursive(); + } + } + + this.StructureChanged(this, new TreePathEventArgs(new TreePath())); + } } public void Modify(ProcessItem oldItem, ProcessItem newItem)