Skip to content

Performance optimization

Below are some useful tactics to follow when you need to deal with huge treeviews with hundreds or thousands of nodes.

Key points#

After changes, treeview will need to recalculate its internal state and redraw itself. All internal changes (add or remove node/node control/column, etc.) will update the treeview automatically, but if you change an external data or want to control the update process (on mass or frequently changes) to minimize performance overhead, you will need to update the treeview manually. Flexible TreeView allows you to do that by using these methods:

  • FullRepaint - completely updates treeview. Use it when you've updated data the treeview isn't familiar with. When executed often, this operation may be a load on your computer's resources depending on what resources you have available. If you find this to be the case, you may not want to use it often. Use the BeginUpdate and EndUpdate methods for frequently updates instead.
  • BeginUpdate - blocks any treeview updates. Usually used before mass and frequently changes.
  • EndUpdate - unblocks treeview updates after it has been locked by the BeginUpdate method.

Performance improvement hints#

In fact, the availability of columns influences the speed of adding and deleting nodes in Flexible TreeView. If there are columns in the tree, when changing node content, it does not need to recalculate maximum width of all nodes in order to show a horizontal scroll, meaning that its work is significantly accelerated.

If the treeview contains no columns, with each change the treeview must recalculate maximum width of all nodes, which can slow work down if there are many nodes.

Tip

In order to accelerate the treeview nodes changes add at least one column, if possible.

External data changes reflection#

Treeview will auto-update itself when any data that it is familiar with has changed. To update the treeview after external data changes, call the FullRepaint method.

Warning

Do not call the FullRepaint method too often because it may hurt the treeview performance!

Example

NodeWithImage node = (NodeWithImage)tree.Nodes[0];
// External data changes here. Treeview doesn't know about these changes.
node.Image.SetResolution(72, 72);
// treeview complete update after changes.
tree.FullRepaint();
Dim node As NodeWithImage = DirectCast(tree.Nodes(0), NodeWithImage)
' External data changes here. Treeview doesn't know about these changes.
node.Image.SetResolution(72, 72)
' treeview complete update after changes.
tree.FullRepaint()

Treeview mass changes performance optimizations#

On mass data changes, like mass nodes insertion or deletion, each such a change may cause the treeview to update. To optimize the treeview update performance, call the * BeginUpdate method before and the EndUpdate* method after these changes. It will suspend any treeview repaints or change notifications between those calls.

Warning

Notice that count of BeginUpdate and EndUpdate methods calls must be equal, otherwise, the treeview goes "blank" until the last EndUpdate method get called.

Example

// deny any updates or notifications
tree.BeginUpdate();

// do mass changes.
for(int i = 0; i < 1000; i++)
{
    Node node=new Node("Node #" + i);
    node.AttachTo(tree);
}

// restore normal update behavior.
tree.EndUpdate();
' deny any updates or notifications
tree.BeginUpdate()

' do mass changes.
For i As Integer = 0 To 999
    Dim node As New Node("Node #" + i)
    node.AttachTo(tree)
Next

' restore normal update behavior.
tree.EndUpdate()

Note that above we didn't call FullRepaint method because our changes are "known" changes to the treeview and it will update itself automatically after last EndUpdate method get called.

Using this technique, treeview will be updated only once.

Also, note that you can call the FullRepaint method between the BeginUpdate/EndUpdate methods as many as you need. This won't hurt the treeview performance.