public ProcessNode(uint pid, ProcessInfo info) { _parent = null; _children = new List <ProcessNode>(); _pid = pid; _info = info; }
public void SetParent(ProcessNode parent) { // Avoid loops: look into the hierarchy for myself. var p = parent; while (p != null) { if (p._pid == _pid) { throw new Exception("Circular loop detected!"); } p = p._parent; } // Ok, perform the binding, but only if not already contained foreach (var node in parent._children) { if (node._pid == _pid) { // Already contained. return; } } parent._children.Add(this); _parent = parent; }
public XmlNode GetHierarchy(XmlDocument doc) { var history = doc.CreateElement("ProcessHistory"); lock (this) { Dictionary <uint, ProcessNode> flatList = new Dictionary <uint, ProcessNode>(); // Load all the children and parents into a flat list of ProcessNode foreach (var e in _processes) { // Check both key and value are into the flatlist ProcessNode child = null; ProcessNode parent = null; if (!flatList.TryGetValue(e.Key, out child)) { ProcessInfo pi = null; if (!_pinfo.TryGetValue(e.Key, out pi)) { pi = ProcessInfo.FromPid(e.Key); } child = new ProcessNode(e.Key, pi); flatList.Add(e.Key, child); } if (!flatList.TryGetValue(e.Value, out parent)) { ProcessInfo pi = null; if (!_pinfo.TryGetValue(e.Value, out pi)) { pi = ProcessInfo.FromPid(e.Value); } parent = new ProcessNode(e.Value, pi); flatList.Add(e.Value, parent); } // Build the relationship child.SetParent(parent); } // At this point the roots are the nodes with parent = NULL foreach (var node in flatList) { if (node.Value.Parent == null) { // This gui is a root node. history.AppendChild(node.Value.ToXml(doc)); } } return(history); } }