Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. delegate invocation

delegate invocation

Scheduled Pinned Locked Moved C#
databasejsonquestionannouncement
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xilefxilef
    wrote on last edited by
    #1

    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?

    M P 2 Replies Last reply
    0
    • X xilefxilef

      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?

      M Offline
      M Offline
      Moreno Airoldi
      wrote on last edited by
      #2

      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!)

      1 Reply Last reply
      0
      • X xilefxilef

        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?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Moreno is likely correct. Don't trigger on the text changed, trigger on data changed.

        X 1 Reply Last reply
        0
        • P PIEBALDconsult

          Moreno is likely correct. Don't trigger on the text changed, trigger on data changed.

          X Offline
          X Offline
          xilefxilef
          wrote on last edited by
          #4

          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.

          M P 2 Replies Last reply
          0
          • X xilefxilef

            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.

            M Offline
            M Offline
            Moreno Airoldi
            wrote on last edited by
            #5

            Can you post a code snippet ?

            2+2=5 for very large amounts of 2 (always loved that one hehe!)

            X 1 Reply Last reply
            0
            • M Moreno Airoldi

              Can you post a code snippet ?

              2+2=5 for very large amounts of 2 (always loved that one hehe!)

              X Offline
              X Offline
              xilefxilef
              wrote on last edited by
              #6

              ******************************************************** 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); } ...

              M 1 Reply Last reply
              0
              • X xilefxilef

                ******************************************************** 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); } ...

                M Offline
                M Offline
                Moreno Airoldi
                wrote on last edited by
                #7

                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, with Visible, Enabled, SuspendLayout() / ResumeLayout() and the WM_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!)

                1 Reply Last reply
                0
                • X xilefxilef

                  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.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  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.

                  X 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    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.

                    X Offline
                    X Offline
                    xilefxilef
                    wrote on last edited by
                    #9

                    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.

                    X 1 Reply Last reply
                    0
                    • X xilefxilef

                      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.

                      X Offline
                      X Offline
                      xilefxilef
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups