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

发布于 2024-09-24 12:55:44 字数 1953 浏览 0 评论 0原文

好的,我有一个用作 Windows 目录树的 TreeView。我让它加载所有目录并正常运行,但它在加载包含许多子目录的目录时暂停了我的 GUI。我正在尝试实现多线程,但我对它很陌生并且运气不佳。

这就是我的 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);
        }
    }
}

每当我展开一个包含大量子目录的目录时,程序就会冻结几秒钟。我想在处理时显示加载消息或动画,但我不确定如何开始多线程。我知道我必须使用 TreeView 的 Dispatcher.BeginInvoke 方法,但除此之外我有点迷失了。

任何帮助将不胜感激!

Okay, I have a TreeView that serves as a directory tree for Windows. I have it loading all the directories and functioning correctly, but it is pausing my GUI while it loads directories with many children. I'm trying to implement multithreading, but am new to it and am having no luck.

This is what I have for my 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.

Any help would be greatly appreciated!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2024-10-01 12:55:44

启动异步进程的最简单方法之一是使用带有 BeginInvoke 的匿名委托。例如,您可以将构造函数中的代码移至单独的方法(例如 RenderTreeView),然后异步调用它以开始新线程,如下所示:

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

这样做的技巧是,任何时候您与异步进程中的任何 UI 元素交互时你需要重新加入主UI线程,否则你会得到关于跨线程访问的异常。这也相对简单。

在 Windows 窗体中是:

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

在 WPF 中是:

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

您确实需要分解代码以使其在方法方面更加灵活。目前,所有内容都捆绑在一种方法中,这使得异步流程的使用和重构变得困难。

更新给你:)

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

One of the easiest ways to start a async process is to use an anonymous delegate with 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:

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. This is relatively straight forward as well.

In Windows Forms it's:

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

In WPF it's:

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;
    }
}
海的爱人是光 2024-10-01 12:55:44

多线程在这里可能没有多大帮助,因为 TreeView 必须在其调度程序线程上更新。

加载大量条目时,TreeView 会暂停。解决此问题的一种方法是将内容存储到镜像 TreeView 结构的对象中,然后以编程方式仅加载 TreeView 的第一层。

当用户单击某个节点时,加载下一级子节点并展开该节点。当该节点折叠时,删除其子节点以节省 TreeView 内存。这对我来说效果很好。对于超大型结构,我使用本地 Sqlite 数据库(通过 System.Data.Sqlite)作为支持存储,即使如此,TreeView 加载速度也很快并且响应迅速。

Multithreading may not help much here because the TreeView has to be updated on it's Dispatcher thread.

TreeViews will pause when loading a large number of entries. 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.

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. 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.

記柔刀 2024-10-01 12:55:44

您还可以查看使用BackgroundWorker;这是在单独线程上执行操作的最简单方法(对我来说:))。

BackgroundWorker 组件概述: http://msdn.microsoft.com/en-us/ Library/8xs8549b.aspx

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

您可以将其与命令模式一起使用,如下所述 -

异步 WPF 命令

You can also look at using 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 Class: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

You can use it with Command pattern as explained here -

Asynchronous WPF Commands

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文