加快将字符串列表加载到Treeview

acu*_*ris 5 c# treeview multithreading winforms

Stackflow和C#newbie在这里!

我在下面有一些代码将一个字符串列表加载到树视图控件中,它运行良好.唯一的问题是速度.当列表很大时,加载需要时间,这不是问题,除了它会暂停UI一段时间.

所以一个例子就是像这样的字符串列表(但更大):

c:\ drivers\test1.txt
c:\ drivers\test2.txt
c:\ drivers\folder\test1.txt
c:\ brother\tester\text1.zip
c:\ brother\another\text2.zip
c:\ data\company1\accounts.rar
c:\ data\company2\accounts.rar

treeview使用反斜杠标记分割字符串,并将它们整齐地放在资源管理器视图中 - 这很好!

tvRestore是示例中的Treeview控件.

foreach (string path in lstKeys)
        {

            lastNode = null;
            subPathAgg = string.Empty;
            foreach (string subPath in path.Split(new string[] { "\\" }, StringSplitOptions.None))
            {
                foreach (string item in subPath.Split(new string[] { "\\" }, StringSplitOptions.None))
                {
                    if (item == "" || item == null)
                    {
                        continue;
                    }
                    subPathAgg += item + "\\";

                    TreeNode[] n = tvRestore.Nodes.Find(subPathAgg, true);

                    if (n.Length > 0)
                    {
                        lastNode = n[0];

                        continue;
                    }
                    else
                    {
                        // lastNode = null;
                    }

                    TreeNode[] nodes = tvRestore.Nodes.Find(subPathAgg, true);
                    if (nodes.Length == 0)
                        if (lastNode == null)
                            lastNode = tvRestore.Nodes.Add(subPathAgg, item);
                        else
                            lastNode = lastNode.Nodes.Add(subPathAgg, item);
                    else
                        lastNode = nodes[0];
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

唯一的问题是速度.我试图使用Threads但代码异常,因为控件在不同的Thread上.我相信我必须调用Nodes.Add但我无法弄清楚如何做到这一点.

理想情况下,代码将在应用程序启动时开始填充树视图,尽管我不希望应用程序锁定大型列表30-40秒或更长时间.

加快这一过程的最佳方法是什么?

Mis*_*goo 5

您可以做一些事情,包括在后台运行它 - 我喜欢使用 Task.Run() 来实现此目的。

private void Form1_Load(object sender, EventArgs e)
{
    // Show the user something
    treeView1.Nodes.Add("Loading...");
    // Run the tree load in the background
    Task.Run(() => LoadTree());
}
Run Code Online (Sandbox Code Playgroud)

然后,您的任务可以构建一个包含所有新节点的 TreeNode,并调用 TreeView 将新节点添加为范围,并使用 BeginUpdate...EndUpdate 来防止视觉更新,直到加载所有节点。

private void LoadTree()
{
    // Get a list of everything under the users' temp folder as an example
    string[] fileList;
    DirectoryInfo df = new DirectoryInfo(Path.GetTempPath());
    fileList = df.GetFiles("*.*",SearchOption.AllDirectories).Select<FileInfo, string>((f) => f.FullName).ToArray();

    // Parse the file list into a TreeNode collection
    TreeNode node = GetNodes(new TreeNode(), fileList);

    // Copy the new nodes to an array
    int nodeCount = node.Nodes.Count;
    TreeNode[] nodes = new TreeNode[nodeCount];
    node.Nodes.CopyTo(nodes, 0);

    // Invoke the treeview to add the nodes
    treeView1.Invoke((Action)delegate ()
    {
        treeView1.BeginUpdate(); // No visual updates until we say 
        treeView1.Nodes.Clear(); // Remove existing nodes
        treeView1.Nodes.AddRange(nodes); // Add the new nodes
        treeView1.EndUpdate(); // Allow the treeview to update visually
    });
}
Run Code Online (Sandbox Code Playgroud)

这就是我构建 TreeNode 列表的方式。

private TreeNode GetNodes(TreeNode parent, string[] fileList)
{
    // build a TreeNode collection from the file list
    foreach (string strPath in fileList)
    {
        // Every time we parse a new file path, we start at the top level again
        TreeNode thisParent = parent;

        // split the file path into pieces at every backslash
        foreach (string pathPart in strPath.Split('\\'))
        {
            // check if we already have a node for this
            TreeNode[] tn = thisParent.Nodes.Find(pathPart, false);

            if (tn.Length == 0)
            {
                // no node found, so add one
                thisParent = thisParent.Nodes.Add(pathPart,pathPart);
            }
            else
            {
                // we already have this node, so use it as the parent of the next part of the path
                thisParent = tn[0];
            }
        }

    }
    return parent;
}
Run Code Online (Sandbox Code Playgroud)

在我的机器上,通过这种方式将 56,000 个节点加载到树视图中大约需要 1.5 秒,并且 UI 不会阻塞。