delegate invocation
-
I have a treeview where multiple nodes are tagged with the same tag object. When one node's text is changed, i'm using the tag object to trigger an event to change the text for all the rest of the nodes tied to this tag object. After having close to 1000 nodes tied to the same tag object, the text update takes forever. I put some print statements inside the delegate method to see how the invocation list is being executed. Well it seems that the more delegate methods registered the slower it goes from each delegate method to the next. Why would an increase in length of the invocation list slow down transition from one delegate method to the next?
-
I have a treeview where multiple nodes are tagged with the same tag object. When one node's text is changed, i'm using the tag object to trigger an event to change the text for all the rest of the nodes tied to this tag object. After having close to 1000 nodes tied to the same tag object, the text update takes forever. I put some print statements inside the delegate method to see how the invocation list is being executed. Well it seems that the more delegate methods registered the slower it goes from each delegate method to the next. Why would an increase in length of the invocation list slow down transition from one delegate method to the next?
I think that's because when you change the other nodes' text you trigger your event again, so that execution time goes up exponentially. You need to inhibit event triggering before you start updating all the nodes, and re-enable it when you're done. :)
2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
I have a treeview where multiple nodes are tagged with the same tag object. When one node's text is changed, i'm using the tag object to trigger an event to change the text for all the rest of the nodes tied to this tag object. After having close to 1000 nodes tied to the same tag object, the text update takes forever. I put some print statements inside the delegate method to see how the invocation list is being executed. Well it seems that the more delegate methods registered the slower it goes from each delegate method to the next. Why would an increase in length of the invocation list slow down transition from one delegate method to the next?
Moreno is likely correct. Don't trigger on the text changed, trigger on data changed.
-
Moreno is likely correct. Don't trigger on the text changed, trigger on data changed.
I'm using AfterLabelEdit to trigger the main text change event. I also put print statements inside the custom UpdateText method of my custom TreeNode, the same UpdateText that is added as a delegate to the invocation list of the eventhandler inside the tag object. The print statements increment a counter each time text is updated. With 40 nodes to update, I get '1, 2, 3,...' and so on fairly quickly. With 1000, it take a while for each counter to print, which indicates longer time between invocation method execution. Any thoughts? thanx.
-
I'm using AfterLabelEdit to trigger the main text change event. I also put print statements inside the custom UpdateText method of my custom TreeNode, the same UpdateText that is added as a delegate to the invocation list of the eventhandler inside the tag object. The print statements increment a counter each time text is updated. With 40 nodes to update, I get '1, 2, 3,...' and so on fairly quickly. With 1000, it take a while for each counter to print, which indicates longer time between invocation method execution. Any thoughts? thanx.
Can you post a code snippet ?
2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
Can you post a code snippet ?
2+2=5 for very large amounts of 2 (always loved that one hehe!)
******************************************************** public class MyTreeNode: TreeNode {... public MyTreeNode(TagObject obj) { ... this.Tag = obj; obj.TextChanged += new EventHandler(UpdateText); ... } public void UpdateTagText(string text) { ((TagObject)this.Tag).Name = text; } public void UpdateText(object sender, EventArgs e) { this.Text = ((TextChangedEventArgs)e).Text; System.Diagnostics.Debug.WriteLine(((TagObject)this.Tag).c++.ToString()); }... *********************************************************************** public class TagObject { public event EventHandler TextChanged; private string name; public int c = 0; ... public string Name { get { return this.name; } set { this.SetName(value); } } // Invoke the TextChanged event when text changes public void OnTextChanged(TextChangedEventArgs e) { if (TextChanged != null) TextChanged(this, e); } private void SetName(string text) { this.name = text; TextChangedEventArgs ea = new TextChangedEventArgs(); ea.Text = text; OnTextChanged(ea); }... } public class TextChangedEventArgs : EventArgs { private string text; public string Text { get { return this.text; } set { this.text = value; } } } ***************************************************************** from the form class that contains my treeview: ... private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { ((MyTreeNode)e.Node).UpdateTagText(e.label); } ...
-
******************************************************** public class MyTreeNode: TreeNode {... public MyTreeNode(TagObject obj) { ... this.Tag = obj; obj.TextChanged += new EventHandler(UpdateText); ... } public void UpdateTagText(string text) { ((TagObject)this.Tag).Name = text; } public void UpdateText(object sender, EventArgs e) { this.Text = ((TextChangedEventArgs)e).Text; System.Diagnostics.Debug.WriteLine(((TagObject)this.Tag).c++.ToString()); }... *********************************************************************** public class TagObject { public event EventHandler TextChanged; private string name; public int c = 0; ... public string Name { get { return this.name; } set { this.SetName(value); } } // Invoke the TextChanged event when text changes public void OnTextChanged(TextChangedEventArgs e) { if (TextChanged != null) TextChanged(this, e); } private void SetName(string text) { this.name = text; TextChangedEventArgs ea = new TextChangedEventArgs(); ea.Text = text; OnTextChanged(ea); }... } public class TextChangedEventArgs : EventArgs { private string text; public string Text { get { return this.text; } set { this.text = value; } } } ***************************************************************** from the form class that contains my treeview: ... private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { ((MyTreeNode)e.Node).UpdateTagText(e.label); } ...
I made a few tests, and it seems the problem is not with delegate invocation. If you disable all events and try this:
DateTime Start = DateTime.Now;
string MyLabel = "Test";
for (int i = 0; i < treeView.Nodes.Count; i++)
{
treeView.Nodes[i].Text = MyLabel;
}
TimeSpan ts = DateTime.Now.Subtract(Start);
MessageBox.Show(ts.TotalSeconds.ToString("0.000"));You'll see it takes just the same time. So, the problem is the time the
TreeNode
component takes to update its text. I tried disabiling on-screen updating, withVisible
,Enabled
,SuspendLayout()
/ResumeLayout()
and theWM_SETREDRAW
window message, but it doesn't seem to gain you time. So, I'm afraid you cannot make it faster unless you design your own TreeView / TreeNode classes optimizing them for text updating. If that's possible. Good luck, let me know if you find any solution. :)2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
I'm using AfterLabelEdit to trigger the main text change event. I also put print statements inside the custom UpdateText method of my custom TreeNode, the same UpdateText that is added as a delegate to the invocation list of the eventhandler inside the tag object. The print statements increment a counter each time text is updated. With 40 nodes to update, I get '1, 2, 3,...' and so on fairly quickly. With 1000, it take a while for each counter to print, which indicates longer time between invocation method execution. Any thoughts? thanx.
I wouldn't use any node (or tree) events to trigger the updates. I'm not sure how you implemented it, but what I envisioned when pondering your post the other week, wouldn't require it.
-
I wouldn't use any node (or tree) events to trigger the updates. I'm not sure how you implemented it, but what I envisioned when pondering your post the other week, wouldn't require it.
But why does the time it takes for node to update its text depend on the number of nodes in a tree? Because if I update node text when only 50 nodes are tagged with the same object is very fast compared to when 1000 are tagged with the same obj.
-
But why does the time it takes for node to update its text depend on the number of nodes in a tree? Because if I update node text when only 50 nodes are tagged with the same object is very fast compared to when 1000 are tagged with the same obj.
ok, solved it. The idea is to have treeview beginupdate in the begginning of the textchanged delegate invocation and endupdate at the end when all nodes are updated. public class TagObject {... public event EventHandler TextChanged; public int TextChangedCount; public bool startOfInvocation; ... public void OnTextChanged(TextChangedEventArgs e) { if (TextChanged != null) { this.TextChangedCount = TextChanged.GetInvocationList().GetLength(0); this.startOfInvocation = true; TextChanged(this, e); } } *******************************************************************8 public class MyTreeNode : TreeNode {... public void UpdateTagText(string text) { ((TagObject)this.Tag).Name = text; }... public void UpdateText(object sender, EventArgs e) { TagObject tagObj = (TagObject)this.Tag; if (tagObj.startOfInvocation) { this.TreeView.BeginUpdate(); tagObj.startOfInvocation = false; } this.Text = ((TextChangedEventArgs)e).Text; tagObj.TextChangedCount--; if (tagObj.TextChangedCount == 0) { this.TreeView.EndUpdate(); } } takes about a sec for 500 nodes.