Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:

C#
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(); });
                }
            }

        }


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




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?
Posted

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.
 
Share this answer
 
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900