如何使用BackgroundWorker填充TreeView [英] How to populate TreeView with BackgroundWorker

查看:63
本文介绍了如何使用BackgroundWorker填充TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个填充树视图的方法。我使用MouseDoubleClick从listview调用此方法。此方法完成所有工作:读取SQL表,生成3 List<> (父,子,孙)和一些if ...和foreach ...循环从这些列表的元素构建树视图。工作正常。



但是!它很慢......我想使用BackgroundWorker显示一些带有该PictureBox可见属性的'加载圈'gif。



我怎么能在DoWork事件中运行此方法?



我有这个DoWork活动,但它不是正确的:



Hi,

I have a method that populates a treeview. I call this method from a listview with MouseDoubleClick. This method does all the job: reads the SQL table, generates 3 List<> (parent, child, grandchild) and some if... and foreach... loops builds the treeview from these lists' elements. Works fine.

But! It is quite slow... I would like to use BackgroundWorker with showing up some 'loading circle' gif with the property of visible of that PictureBox.

How can I run this method within a DoWork event?

I have this DoWork event, but it is not corrent:

private void loading_DoWork(object sender, DoWorkEventArgs e)
        {

            if (loading.CancellationPending == true)
            {
                loadingPicBox.Visible = false;
                e.Cancel = true;
            }
            else
            {
                if(loadingPicBox.InvokeRequired)
                {
                    loadingPicBox.Invoke((MethodInvoker)delegate () { loadingPicBox.Visible = true; });
                    Thread.Sleep(100);//if this is not here, I cannot see the gif...
                }
                if (dokuSiteListView.InvokeRequired)
                {
                    dokuSiteListView.Invoke((MethodInvoker)delegate () { BuildTree(); });
                }
            }

        }







private void dokuSiteListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!loading.IsBusy)
            {
                loading.RunWorkerAsync();
            }
        }









As你可以看到,如果我双击dokuSiteListView中的一个项目,我调用loading.RunWorkerAsync();, loadingPicBox变为可见,并且构建树正在启动。但是当gif动画开始时,gif动画停止了...



有人可以给我正确的代码来从后台工作者调用我的BuildTree()方法吗?





As you can see, if I doubleclick on an item in dokuSiteListView, I call loading.RunWorkerAsync();, the loadingPicBox become visible, and building tree is starting. But the gif animation stops as the tree building start...

Can somebody please give me the right code to call my BuildTree() method from a backgroundworker?

推荐答案

请参考:如何:使用支持基于事件的异步模式的组件 [ ^ ]。在那里你会找到一个部分:启用PictureBox控件以异步加载图像。按照相关页面底部的链接。
Please, refer this: How to: Use Components That Support the Event-based Asynchronous Pattern[^]. There you'll find a section: To enable a PictureBox control to asynchronously load an image. Follow the links on the bottom of related page.


在后台填充树视图是一种方法。但是,如果树很大,您是否真的需要将每个节点添加到树中或仅添加用户扩展的节点。如果树很大并且使UI执行缓慢,则将所有节点添加到树可能会导致内存问题。



所以我建议只需添加顶级节点和占位符作为包含子节点的节点的子节点。现在,当展开具有占位符作为子节点的节点时,添加实际子节点以及包含子节点的节点的占位符。这样,树只会填充所需的部分。



如果你有兴趣,我在下面的文章中使用过这种方法,所以如果你检查代码你看到这个有效:目录大小浏览器 [ ^ ]
Populating the tree view on the background is one approach. However, if the tree is large, do you really need to add every node into the tree or just the ones user expands. Adding all nodes to the tree may cause memory issues if the tree is large and make the UI perform sluggishly.

So what I suggest is that just add the top level nodes and a placeholder as a child node for the nodes that contain children. Now when a node with a placeholder as child is expanded, add the actual children and again the placeholders for the nodes that contain children. This way the tree is populated only on needed parts.

If you're interested, I've used this approach in the following article so if you examine the code you see this in effect: Directory size browser[^]


这篇关于如何使用BackgroundWorker填充TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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