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