Results 1 to 8 of 8

Thread: [RESOLVED] TreeView -- Load nodes with multithreading

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Resolved [RESOLVED] TreeView -- Load nodes with multithreading

    I have a treeview control with 3 levels of nodes. Domains, users and folders.

    When a node gets expanded, I want to load its children dynamically and simultaneously calculate the size of each child in a separate thread so that it will not freeze the GUI.


    Code:
            public void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
            {
                switch (e.Node.Level)  //Domain level
                {
                    case 0:  
    
                        string domin = e.Node.Name;
                        List<string> userList = getUsers(domain);
    		                      
                        foreach (string user in userList)
                        {
                                                    
                            TreeNode temp = new TreeNode();
                            temp.Name = user
                            temp.Text = user + " (Calculating size) ";
    
                            ThreadContainer1 threadcontainer = new ThreadContainer1(e.Node, domain, user);
                            Thread thread1 = new Thread(new ThreadStart(threadcontainer.GetUserSize));
                            thread1.Start();
    
                            e.Node.Nodes.Add(temp);
                        }                    
    
                        break;
    
                    case 1: //User level
    		    string domain = e.Node.Parent.Name;
    		    string user = e.Node.Name;
                       
                        List<string> folderList = getFolders(domain, user);
    
                        foreach (string folder in folders)
                        {
                           
                            TreeNode temp = new TreeNode();                        
                            temp.Name = folder;
    			temp.Text = user + " (Calculating size) ";
    
    			ThreadContainer2 threadcontainer = new ThreadContainer2(e.Node, domain, user, folder);
                            Thread thread1 = new Thread(new ThreadStart(threadcontainer.GetUFolderSize));
    			thread1.Start();                        
                           
                            e.Node.Nodes.Add(temp);
                        }
    
                        break;
    
                }           
    
            }
    Each thread is supposed to update the referenced node to display the corresponding size after calculating for a while.

    However "Cross-thread operation not valid with threading" exception gets thrown whenever a thread tries to access the referenced node.

    I googled the issue and found out about delegate.
    http://www.shabdar.org/cross-thread-...not-valid.html

    But the problem is that node is not a control. So it does not have Invoke() method.

    Anyone knows how to solve this problem?
    Last edited by winterslam; Jul 2nd, 2009 at 11:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width