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.
Each thread is supposed to update the referenced node to display the corresponding size after calculating for a while.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; } }
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?




Reply With Quote