Treeview loading with BeginUpdate&EndUpdate

F

Freesc

Hi there~

As is known to all, the Treeview::BeginUpdate/EndUpdate method is used
to update or reload the treeview without redrawing the treenode one by
one,and it can save some performance for us.But here i'm writing an
example that,however,doesn's make any difference with or without these
two method.Here's my code:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

void TestTreeViewLoad(bool async)
{
int start = Environment.TickCount;
int end = 0;
//
// Use the BeginUpdate/EndUpdate asynchronized or not?
//
//if (async)
treeView1.BeginUpdate();

treeView1.Nodes.Clear();
end = Environment.TickCount;
Debug.WriteLine("Treeview Clear time (ms): " + (end -
start));
//
// Root node
//
TreeNode tn = new TreeNode(@"\");
treeView1.Nodes.Add(tn);
DirectoryInfo rootDir = new DirectoryInfo(@"\");
end = Environment.TickCount;
Debug.WriteLine("Treeview Addtoot time (ms): " + (end -
start));
//
// Update the TreeView control with the directory tree
//
UpdateTreeView(tn, rootDir);
treeView1.Nodes[0].Expand();
end = Environment.TickCount;
Debug.WriteLine("Treeview update time (ms): " + (end -
start));

//if (async)
// treeView1.EndUpdate();
treeView1.EndUpdate();
end = Environment.TickCount;

Debug.WriteLine("Treeview load time (ms): " + (end -
start));
}

//
// Recursively update tree view
//
private void UpdateTreeView(TreeNode tn, DirectoryInfo dir)
{
//
// Display folder names; Recursively call the method
//
foreach (DirectoryInfo di in dir.GetDirectories())
{
TreeNode tn2 = new TreeNode(di.Name);
tn.Nodes.Add(tn2);
UpdateTreeView(tn2, di);
}

//
// Display file names
//
foreach (FileInfo fi in dir.GetFiles())
{
TreeNode tn3 = new TreeNode(fi.Name);
tn.Nodes.Add(tn3);
}
}
private void menuItem1_Click(object sender, EventArgs e)
{
TestTreeViewLoad(true);
}
}

*********Output with aysnc update**********
Treeview Addtoot time (ms): 687
Treeview update time (ms): 5177
Treeview load time (ms): 5260

*********Output without aysnc update**********
Treeview Addtoot time (ms): 697
Treeview update time (ms): 5197
Treeview load time (ms): 5280

Obviously,the update operation takes a long time,and it seems that
BeginUpdate&EndUpdate doesn't work...

Any suggustion about that?

Thanks in advance

Regards
Freesc
 
F

Freesc

Hi there~

As is known to all, the Treeview::BeginUpdate/EndUpdate method is used
to update or reload the treeview without redrawing the treenode one by
one,and it can save some performance for us.But here i'm writing an
example that,however,doesn's make any difference with or without these
two method.Here's my code:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void TestTreeViewLoad(bool async)
        {
            int start = Environment.TickCount;
            int end = 0;
            //
            // Use the BeginUpdate/EndUpdate asynchronized or not?
            //
            //if (async)
          treeView1.BeginUpdate();

            treeView1.Nodes.Clear();
            end = Environment.TickCount;
           Debug.WriteLine("Treeview Clear time (ms): " + (end-
start));
            //
            // Root node
            //
            TreeNode tn = new TreeNode(@"\");
            treeView1.Nodes.Add(tn);
            DirectoryInfo rootDir = new DirectoryInfo(@"\");
            end = Environment.TickCount;
            Debug.WriteLine("Treeview Addtoot time (ms): " + (end -
start));
            //
            // Update the TreeView control with the directory tree
            //
            UpdateTreeView(tn, rootDir);
            treeView1.Nodes[0].Expand();
            end = Environment.TickCount;
            Debug.WriteLine("Treeview update time (ms): " + (end -
start));

            //if (async)
            //    treeView1.EndUpdate();
            treeView1.EndUpdate();
            end = Environment.TickCount;

            Debug.WriteLine("Treeview load time (ms): " + (end-
start));
        }

        //
        // Recursively update tree view
        //
        private void UpdateTreeView(TreeNode tn, DirectoryInfo dir)
        {
            //
            // Display folder names; Recursively call the method
            //
            foreach (DirectoryInfo di in dir.GetDirectories())
            {
                TreeNode tn2 = new TreeNode(di.Name);
                tn.Nodes.Add(tn2);
                UpdateTreeView(tn2, di);
            }

            //
            // Display file names
            //
            foreach (FileInfo fi in dir.GetFiles())
            {
                TreeNode tn3 = new TreeNode(fi.Name);
                tn.Nodes.Add(tn3);
            }
        }
        private void menuItem1_Click(object sender, EventArgs e)
        {
            TestTreeViewLoad(true);
        }
    }

*********Output with aysnc update**********
Treeview Addtoot time (ms): 687
Treeview update time (ms): 5177
Treeview load time (ms): 5260

*********Output without aysnc update**********
Treeview Addtoot time (ms): 697
Treeview update time (ms): 5197
Treeview load time (ms): 5280

Obviously,the update operation takes a long time,and it seems that
BeginUpdate&EndUpdate doesn't work...

Any suggustion about that?

Thanks in advance

Regards
Freesc

In addition,
i'm using IBM thinkpad X60+VS2008+.NET CF3.5+WM6 Standard Emulator for
developing and test

:)

Regards
 
J

Jin Chang

Hi there~

As is known to all, the Treeview::BeginUpdate/EndUpdate method is used
to update or reload the treeview without redrawing the treenode one by
one,and it can save some performance for us.But here i'm writing an
example that,however,doesn's make any difference with or without these
two method.Here's my code:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

void TestTreeViewLoad(bool async)
{
int start = Environment.TickCount;
int end = 0;
//
// Use the BeginUpdate/EndUpdate asynchronized or not?
//
//if (async)
treeView1.BeginUpdate();

treeView1.Nodes.Clear();
end = Environment.TickCount;
Debug.WriteLine("Treeview Clear time (ms): " + (end -
start));
//
// Root node
//
TreeNode tn = new TreeNode(@"\");
treeView1.Nodes.Add(tn);
DirectoryInfo rootDir = new DirectoryInfo(@"\");
end = Environment.TickCount;
Debug.WriteLine("Treeview Addtoot time (ms): " + (end -
start));
//
// Update the TreeView control with the directory tree
//
UpdateTreeView(tn, rootDir);
treeView1.Nodes[0].Expand();
end = Environment.TickCount;
Debug.WriteLine("Treeview update time (ms): " + (end -
start));

//if (async)
// treeView1.EndUpdate();
treeView1.EndUpdate();
end = Environment.TickCount;

Debug.WriteLine("Treeview load time (ms): " + (end -
start));
}

//
// Recursively update tree view
//
private void UpdateTreeView(TreeNode tn, DirectoryInfo dir)
{
//
// Display folder names; Recursively call the method
//
foreach (DirectoryInfo di in dir.GetDirectories())
{
TreeNode tn2 = new TreeNode(di.Name);
tn.Nodes.Add(tn2);
UpdateTreeView(tn2, di);
}

//
// Display file names
//
foreach (FileInfo fi in dir.GetFiles())
{
TreeNode tn3 = new TreeNode(fi.Name);
tn.Nodes.Add(tn3);
}
}
private void menuItem1_Click(object sender, EventArgs e)
{
TestTreeViewLoad(true);
}
}

*********Output with aysnc update**********
Treeview Addtoot time (ms): 687
Treeview update time (ms): 5177
Treeview load time (ms): 5260

*********Output without aysnc update**********
Treeview Addtoot time (ms): 697
Treeview update time (ms): 5197
Treeview load time (ms): 5280

Obviously,the update operation takes a long time,and it seems that
BeginUpdate&EndUpdate doesn't work...

Any suggustion about that?

Thanks in advance

Regards
Freesc

I think you're reading into BeginUpdate and EndUpdate a bit too much.
Your example seems to indicate the that majority of your run time is
due to retrieving the directory information. The advantages of
BeginUpdate and EndUpdate functions will be more obvious if the
information being loaded is, for example, from a memory variable like
an array. These function simply prevent the control from updating as
the items are added or modified, thus eliminating the unnecessary
visual processing. Depending on the amount of data involved, this may
or may not have noticeable impact on the performance.
 
F

Freesc

Hi there~
As is known to all, the Treeview::BeginUpdate/EndUpdate method is used
to update or reload the treeview without redrawing the treenode one by
one,and it can save some performance for us.But here i'm writing an
example that,however,doesn's make any difference with or without these
two method.Here's my code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void TestTreeViewLoad(bool async)
        {
            int start = Environment.TickCount;
            int end = 0;
            //
            // Use the BeginUpdate/EndUpdate asynchronized or not?
            //
            //if (async)
          treeView1.BeginUpdate();
            treeView1.Nodes.Clear();
            end = Environment.TickCount;
           Debug.WriteLine("Treeview Clear time (ms): " + (end -
start));
            //
            // Root node
            //
            TreeNode tn = new TreeNode(@"\");
            treeView1.Nodes.Add(tn);
            DirectoryInfo rootDir = new DirectoryInfo(@"\");
            end = Environment.TickCount;
            Debug.WriteLine("Treeview Addtoot time (ms): " +(end -
start));
            //
            // Update the TreeView control with the directory tree
            //
            UpdateTreeView(tn, rootDir);
            treeView1.Nodes[0].Expand();
            end = Environment.TickCount;
            Debug.WriteLine("Treeview update time (ms): " + (end -
start));
            //if (async)
            //    treeView1.EndUpdate();
            treeView1.EndUpdate();
            end = Environment.TickCount;
            Debug.WriteLine("Treeview load time (ms): " + (end -
start));
        }
        //
        // Recursively update tree view
        //
        private void UpdateTreeView(TreeNode tn, DirectoryInfo dir)
        {
            //
            // Display folder names; Recursively call the method
            //
            foreach (DirectoryInfo di in dir.GetDirectories())
            {
                TreeNode tn2 = new TreeNode(di.Name);
                tn.Nodes.Add(tn2);
                UpdateTreeView(tn2, di);
            }
            //
            // Display file names
            //
            foreach (FileInfo fi in dir.GetFiles())
            {
                TreeNode tn3 = new TreeNode(fi.Name);
                tn.Nodes.Add(tn3);
            }
        }
        private void menuItem1_Click(object sender, EventArgs e)
        {
            TestTreeViewLoad(true);
        }
    }
*********Output with aysnc update**********
Treeview Addtoot time (ms): 687
Treeview update time (ms): 5177
Treeview load time (ms): 5260
*********Output without aysnc update**********
Treeview Addtoot time (ms): 697
Treeview update time (ms): 5197
Treeview load time (ms): 5280
Obviously,the update operation takes a long time,and it seems that
BeginUpdate&EndUpdate doesn't work...
Any suggustion about that?
Thanks in advance
Regards
Freesc

I think you're reading into BeginUpdate and EndUpdate a bit too much.
Your example seems to indicate the that majority of your run time is
due to retrieving the directory information.  The advantages of
BeginUpdate and EndUpdate functions will be more obvious if the
information being loaded is, for example, from a memory variable like
an array.  These function simply prevent the control from updating as
the items are added or modified, thus eliminating the unnecessary
visual processing.  Depending on the amount of data involved, this may
or may not have noticeable impact on the performance.- Hide quoted text -

- Show quoted text -

Jin,
Thanks for your reply.
I think i got some twisted about BeginUpdate/EndUpdate, and now i'm
clear :)
the majority of the performance is due to retrieving the directory
information,not the refreshing of the treeview

thx & regards
Freesc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top