TreeView控件 - ContextSwitchDeadlock解决方法 [英] Treeview Control - ContextSwitchDeadlock workarounds

查看:138
本文介绍了TreeView控件 - ContextSwitchDeadlock解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个TreeView控件列出任何驱动器或文件夹的目录结构。但是,如果你选择一个驱动器,或者一些与文件夹和子文件夹中的一个大的结构控制需要很长的时间来加载,并在某些情况下显示了MDA ContextSwitchDeadlock消息。我已经禁用了MDA死锁错误消息和它的作品,但我不喜欢的时间因素和应用程序看起来像它已被锁定。我怎么可以修改代码,使其不断抽水的消息,而非缓冲整个视图,并通过在其整体的控制,有没有推动它,要控制,因为它是正在兴建的一种方式?

I have built a treeview control that lists the directory structure of any drive or folder. However, if you select a drive, or something with a large structure of folders and sub folders the control takes a long time to load and in some instances shows an MDA ContextSwitchDeadlock message. I have disabled the MDA deadlock error message and it works, but I don't like the time factor and the app looking like it has locked. How can I modify the code so that it keeps pumping messages, and rather than buffering the whole view and passing it in its entirety to the control, is there a way of pushing it, to the control, as it is being built?

//Call line
treeView1.Nodes.Add(TraverseDirectory(source_computer_fldbrowser.SelectedPath));

private TreeNode TraverseDirectory(string path)
    {
        TreeNode result;
        try
        {
            string[] subdirs = Directory.GetDirectories(path);
            result = new TreeNode(path);
            foreach (string subdir in subdirs)
            {
                TreeNode child = TraverseDirectory(subdir);
                if (child != null) { result.Nodes.Add(child); }
            }
            return result;
        }
        catch (UnauthorizedAccessException)
        {
            // ignore dir
            result = null;
        }
        return result;
    }



由于R上。

Thanks R.

推荐答案

如果您不需要在TreeView加载的整体结构,但只能看到正在扩大,你可以这样来做:

If you not need the whole structure loaded in the TreeView but only see what is being expanded, you can do it this way:

// Handle the BeforeExpand event
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
   if (e.Node.Tag != null) {
       AddTopDirectories(e.Node, (string)e.Node.Tag);
   }
}

private void AddTopDirectories(TreeNode node, string path)
{
    node.BeginUpdate(); // for best performance
    node.Nodes.Clear(); // clear dummy node if exists

    try {
        string[] subdirs = Directory.GetDirectories(path);

        foreach (string subdir in subdirs) {
            TreeNode child = new TreeNode(subdir);
            child.Tag = subdir; // save dir in tag

            // if have subdirs, add dummy node
            // to display the [+] allowing expansion
            if (Directory.GetDirectories(subdir).Length > 0) {
                child.Nodes.Add(new TreeNode()); 
            }
            node.Nodes.Add(child);
        }
    } catch (UnauthorizedAccessException) { // ignore dir
    } finally {
        node.EndUpdate(); // need to be called because we called BeginUpdate
        node.Tag = null; // clear tag
    }
}



呼叫线将是:

The call line will be:

TreeNode root = new TreeNode(source_computer_fldbrowser.SelectedPath);
AddTopDirectories(root, source_computer_fldbrowser.SelectedPath);
treeView1.Nodes.Add(root);

这篇关于TreeView控件 - ContextSwitchDeadlock解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆