private void afterDisconnect() { mDisconnectPending = false; TreeModel model = (treeViewAdv1.Model as SortedTreeModel).InnerModel as TreeModel; mClient.Close(); mClient = null; textBox1.Enabled = true; button1.Text = "Connect"; if (mPasued) button2_Click(this, new EventArgs()); button2.Enabled = false; model.Nodes.Clear(); mRootNode = null; }
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { // Split on ';'. // If you want to support function name has ";", a more robust regular expression is needed. Regex regex = new Regex(@";"); mStringList.Clear(); mReportString = ""; string lastLine = ""; if (mRootNode == null) mRootNode = new CallstackNode(); CallstackNode node = null; CallstackNode previousNode = null; // Back up the node of last iteration, for detecting dead nodes. // Parse the incomming string message and build a corresponding callstack tree. while(true) { if (backgroundWorker1.CancellationPending) break; string s = mStreamReader.ReadLine(); // We use double new line as a terminate indicator if (s == lastLine && s == "") break; if (mPasued) continue; if (s.Length > 0) { string[] tokens = regex.Split(s); int level = Int32.Parse(tokens[0]); string id = tokens[1]; if (node == null) node = mRootNode; else { previousNode = node; // Up the callstack for (int j = node.Level - level; j > -1; ) { // NOTE: node.Level - node.Parent.Level may not simply equals to 1 j -= (node.Level - node.Parent.Level); node = node.Parent as CallstackNode; } // Search for existing node with the same name CallstackNode searchNode = null; foreach (CallstackNode n in node.Nodes) { if (n.Id == id) { searchNode = n; break; } } // Create a new node if none has found if (searchNode == null) { searchNode = new CallstackNode(); searchNode.Parent = node; } node = searchNode; } // Remove any "dead" node if (previousNode != null) { while (previousNode.MyNextNode != null && previousNode.MyNextNode != node) previousNode.MyNextNode.Parent = null; } node.MyPreviousNode = previousNode; node.Level = level; node.Id = tokens[1]; node.Name = tokens[2]; node.TCount = Int32.TryParse(tokens[3], out node.TCount) ? node.TCount : -1; node.SCount = Int32.TryParse(tokens[4], out node.SCount) ? node.SCount : -1; node.TkBytes = Double.Parse(tokens[5]); node.SkBytes = Double.Parse(tokens[6]); node.SCountPerFrame = Double.TryParse(tokens[7], out node.SCountPerFrame) ? node.SCountPerFrame : -1; node.CallPerFrame = Double.TryParse(tokens[8], out node.CallPerFrame) ? node.CallPerFrame : -1; } lastLine = s; mStringList.Add(s); mReportString += s; } // while(true) // Remove any "dead" node { while (node != null && node.MyNextNode != null) node.MyNextNode.Parent = null; } } catch(Exception ex) { MessageBox.Show(ex.ToString()); disconnect(); } }