简体   繁体   English

需要帮助在我的TreeView中实现多线程(C#,WPF)

[英]Need help implementing multithreading into my TreeView (C#, WPF)

Okay, I have a TreeView that serves as a directory tree for Windows. 好的,我有一个TreeView可以用作Windows的目录树。 I have it loading all the directories and functioning correctly, but it is pausing my GUI while it loads directories with many children. 我可以加载所有目录并正常运行,但是它在加载具有许多子级的目录时暂停了我的GUI。 I'm trying to implement multithreading, but am new to it and am having no luck. 我正在尝试实现多线程,但是它是新手,没有运气。

This is what I have for my TreeView: 这是我的TreeView所拥有的:

private readonly object _dummyNode = null;

public MainWindow()
{
    InitializeComponent();

    foreach (string drive in Directory.GetLogicalDrives())
    {
        DriveInfo Drive_Info = new DriveInfo(drive);                

        if (Drive_Info.IsReady == true)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = drive;
            item.Tag = drive;
            item.Items.Add(_dummyNode);
            item.Expanded += folder_Expanded;

            TreeViewItemProps.SetIsRootLevel(item, true);

            Dir_Tree.Items.Add(item);
        }
    }
}

private void folder_Expanded(object sender, RoutedEventArgs e)
{
    TreeViewItem item = (TreeViewItem)sender;

    if (item.Items.Count == 1 && item.Items[0] == _dummyNode)
    {
        item.Items.Clear();

        try
        {
            foreach (string dir in Directory.GetDirectories(item.Tag as string))
            {
                DirectoryInfo tempDirInfo = new DirectoryInfo(dir);

                bool isSystem = ((tempDirInfo.Attributes & FileAttributes.System) == FileAttributes.System);

                if (!isSystem)
                {
                    TreeViewItem subitem = new TreeViewItem();
                    subitem.Header = tempDirInfo.Name;
                    subitem.Tag = dir;
                    subitem.Items.Add(_dummyNode);
                    subitem.Expanded += folder_Expanded;
                    subitem.ToolTip = dir;
                    item.Items.Add(subitem);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

Whenever I expand a directory that has a large number of subdirectories, the program appears to be frozen for a few seconds. 每当我扩展包含大量子目录的目录时,该程序似乎就会冻结几秒钟。 I would like to display a loading message or animation while it's processing, but I'm not sure how to begin with multithreading. 我想在处理过程中显示加载消息或动画,但是我不确定如何从多线程开始。 I know I have to use the TreeView's Dispatcher.BeginInvoke method, but other than that I'm kinda lost. 我知道我必须使用TreeView的Dispatcher.BeginInvoke方法,但除此之外,我有点迷失了。

Any help would be greatly appreciated!!! 任何帮助将不胜感激!!!

One of the easiest ways to start a async process is to use an anonymous delegate with BeginInvoke. 启动异步过程最简单的方法之一是对BeginInvoke使用匿名委托。 As an example you could move your code in the constructor to a separate method (say RenderTreeView) and then call it asynchronously to begin a new thread as follows: 例如,您可以将构造函数中的代码移动到单独的方法(例如RenderTreeView),然后异步调用它以开始一个新线程,如下所示:

Action action = RenderTreeView;
action.BeginInvoke(null, null);

The trick to this is that any time you interact with any UI elements from the async process you need to rejoin the main UI thread, otherwise you will get an exception about cross thread access. 诀窍在于,每次与异步流程中的任何UI元素进行交互时,都需要重新加入主UI线程,否则您将获得有关跨线程访问的异常。 This is relatively straight forward as well. 这也是相对简单的。

In Windows Forms it's: 在Windows窗体中,它是:

if (InvokeRequired)
    Invoke(new MethodInvoker({item.Items.Add(subitem)}));
else
    item.Items.Add(subitem);

In WPF it's: 在WPF中,它是:

if (!Dispatcher.CheckAccess())
    Dispatcher.Invoke(new Action(() => item.Items.Add(subitem)));
else
    item.Items.Add(subitem);

You really need to break up the code to make it more flexible in terms of methods. 您确实需要分解代码以使其在方法方面更加灵活。 At the moment everything is bundled in one method which makes it hard to work with and re-factor for async processes. 目前,所有内容都捆绑在一种方法中,这使得处理异步流程和对其进行重构非常困难。

Update Here you go :) 更新这里,你去:)

public partial class MainWindow : Window
{
    private readonly object dummyNode = null;

    public MainWindow()
    {
        InitializeComponent();

        Action<ItemCollection> action = RenderTreeView;
        action.BeginInvoke(treeView1.Items, null, null);
    }

    private void RenderTreeView(ItemCollection root)
    {
        foreach (string drive in Directory.GetLogicalDrives())
        {
            var driveInfo = new DriveInfo(drive);

            if (driveInfo.IsReady)
            {
                CreateAndAppendTreeViewItem(root, drive, drive, drive);
            }
        }
    }

    private void FolderExpanded(object sender, RoutedEventArgs e)
    {
        var item = (TreeViewItem) sender;

        if (item.Items.Count == 1 && item.Items[0] == dummyNode)
        {
            item.Items.Clear();
            var directory = item.Tag as string;
            if (string.IsNullOrEmpty(directory))
            {
                return;
            }
            Action<TreeViewItem, string> action = ExpandTreeViewNode;
            action.BeginInvoke(item, directory, null, null);
        }
    }

    private void ExpandTreeViewNode(TreeViewItem item, string directory)
    {
        foreach (string dir in Directory.GetDirectories(directory))
        {
            var tempDirInfo = new DirectoryInfo(dir);

            bool isSystem = ((tempDirInfo.Attributes & FileAttributes.System) == FileAttributes.System);

            if (!isSystem)
            {
                CreateAndAppendTreeViewItem(item.Items, tempDirInfo.Name, dir, dir);
            }
        }
    }

    private void AddChildNodeItem(ItemCollection collection, TreeViewItem subItem)
    {
        if (Dispatcher.CheckAccess())
        {
            collection.Add(subItem);
        }
        else
        {
            Dispatcher.Invoke(new Action(() => AddChildNodeItem(collection, subItem)));
        }
    }

    private void CreateAndAppendTreeViewItem(ItemCollection items, string header, string tag, string toolTip)
    {
        if (Dispatcher.CheckAccess())
        {
            var subitem = CreateTreeViewItem(header, tag, toolTip);
            AddChildNodeItem(items, subitem);
        }
        else
        {
            Dispatcher.Invoke(new Action(() => CreateAndAppendTreeViewItem(items, header, tag, toolTip)));
        }
    }

    private TreeViewItem CreateTreeViewItem(string header, string tag, string toolTip)
    {
        var treeViewItem = new TreeViewItem {Header = header, Tag = tag, ToolTip = toolTip};

        treeViewItem.Items.Add(dummyNode);
        treeViewItem.Expanded += FolderExpanded;

        return treeViewItem;
    }
}

Multithreading may not help much here because the TreeView has to be updated on it's Dispatcher thread. 多线程在这里可能无济于事,因为TreeView必须在其Dispatcher线程上进行更新。

TreeViews will pause when loading a large number of entries. 加载大量条目时,TreeView将暂停。 One way to get around this is to store the contents into an object that mirrored the TreeView structure, and then programmatically load just the first level of the TreeView. 解决此问题的一种方法是将内容存储到镜像TreeView结构的对象中,然后以编程方式仅加载TreeView的第一级。

When a user clicks on a node, load the next level of child nodes and expand the node. 当用户单击某个节点时,加载下一级子节点并展开该节点。 When that node is collapsed, delete its child nodes to conserve TreeView memory. 当该节点折叠时,删除其子节点以节省TreeView内存。 This has worked well for me. 这对我来说效果很好。 For exceedingly large structures I've used a local Sqlite database (via System.Data.Sqlite ) as my backing store and even then the TreeView loaded quickly and was responsive. 对于超大型结构,我使用了本地Sqlite数据库(通过System.Data.Sqlite )作为后备存储,即使如此,TreeView仍快速加载并且响应迅速。

You can also look at using BackgroundWorker; 您还可以查看使用BackgroundWorker。 that's easiest way of executing an operation on a separate thread(For me :) ). 这是在单独的线程上执行操作的最简单方法(对我来说:))。

BackgroundWorker Component Overview: http://msdn.microsoft.com/en-us/library/8xs8549b.aspx BackgroundWorker组件概述: http : //msdn.microsoft.com/zh-cn/library/8xs8549b.aspx

BackgroundWorker Class: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx BackgroundWorker类: http : //msdn.microsoft.com/zh-cn/library/system.componentmodel.backgroundworker.aspx

You can use it with Command pattern as explained here - 您可以将其与Command模式结合使用,如此处所述-

Asynchronous WPF Commands 异步WPF命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM