c# - 加快将字符串列表加载到 Treeview 的速度

标签 c# multithreading winforms treeview

这里是 Stackflow 和 C# 新手!

我在下面有一些代码可以将字符串列表加载到 TreeView 控件中,并且效果很好。唯一的问题是速度。当列表很大时,加载需要时间,这不是问题,除非它会锁定 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 使用反斜杠标记拆分字符串,并将它们整齐地放在资源管理器 View 中 - 这很好!

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];
                }
            }
        }

唯一的问题是速度。我尝试使用线程,但代码异常,因为控件位于不同的线程上。我相信我必须调用 Nodes.Add,但我不知道该怎么做。

理想情况下,代码将在应用程序启动时开始填充 TreeView ,尽管我不希望应用程序锁定 30-40 秒或更长的时间以获取更大的列表。

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

最佳答案

您可以做几件事,包括在后台运行它 - 我喜欢为此使用 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());
}

然后您的任务可以构建一个包含所有新节点的 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
    });
}

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

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

在我的机器上,以这种方式将 56,000 个节点加载到 TreeView 中大约需要 1.5 秒,并且 UI 没有阻塞。

关于c# - 加快将字符串列表加载到 Treeview 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40009277/

相关文章:

c# - StreamWriter 中的特殊字符

java - JAVA中有多少种锁

vb.net - 表单关闭时在表单之间传递数据

winforms - 如何使用 NuGet 工作流程避免因程序集版本不匹配而导致的 Winforms 设计器错误

c# - 使用 NewtonSoft 解析 JSON 时出错

c# - RegLoadAppKey 在 32 位操作系统上运行良好,在 64 位操作系统上失败,即使两个进程都是 32 位的

c# - 服务器中的 zip 文件

java - 将项目添加到另一个线程中的列表时进行 CheckForCommodification

java - 如何在 Java 中启动/停止/重新启动线程?

c# - 尝试获取组合框的值时出现 "Cross-thread operation is not valid"异常