In the application im working on right now i have to have a tree view with the paths to registries i have read from a .reg file. That works fine, i have an array with every path found in the file. But the issues come with adding the paths to the tree view. I found a handy function online for plotting a list of paths to the tree. And it actually works fine, but even just plotting 500 paths to the tree takes a LOONG time. I tried optimizing it a bit but it still takes to long to plot every single registry like in the Windows registry editor. And so im pretty bewildered by how they even made the tree view in the registry editor work.
I tried replacing all the foreach loops in the tree plotting function with for loops but it didnt help. Then i tried replacing the for loops with Parallel.For but it simply didnt work. And i tried making my own function that plots a list of paths but i couldnt figure it out. And lastly i tried elevating the applications cpu priority and setting it to x86 and bringing it to release and not debug. It barely helped
I was thinking about having a system where it only plots the items you have expanded. but my brain is not big enough to figure this out.
Should i spent more time of my life figuring that out or is there a better way? Or is there a solution i havent found.
Since somebody asked for code here you go:
private void PopulateTreeView(TreeView treeView, string[] paths)
{
TreeNode lastNode = null;
string subPathAgg;
for (int x = 0; x < paths.Count(); x )
{
subPathAgg = string.Empty;
string[] tmpstr = paths[x].Split(@"\".ToCharArray());
for (int y = 0; y < tmpstr.Count(); y )
{
subPathAgg = tmpstr[y] (@"\".ToCharArray());
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, tmpstr[y]);
else
lastNode = lastNode.Nodes.Add(subPathAgg, tmpstr[y]);
else
lastNode = nodes[0];
}
}
}
CodePudding user response:
You could use the BeginUpdate which suppresses the repainting.
private void PopulateTreeView(TreeView treeView, string[] paths)
{
treeView.BeginUpdate();
try
{
TreeNode lastNode = null;
string subPathAgg;
for (int x = 0; x < paths.Count(); x )
{
subPathAgg = string.Empty;
string[] tmpstr = paths[x].Split(@"\".ToCharArray());
for (int y = 0; y < tmpstr.Count(); y )
{
subPathAgg = tmpstr[y] (@"\".ToCharArray());
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, tmpstr[y]);
else
lastNode = lastNode.Nodes.Add(subPathAgg, tmpstr[y]);
else
lastNode = nodes[0];
}
}
}
finally
{
treeView.EndUpdate();
}
}
I'd rather use the try/finally here. When something goes wrong. at least the treeview will be invalidated.