Question Populate TreeView in Background worker

Developer111

Member
Joined
Apr 21, 2010
Messages
24
Programming Experience
5-10
In vb2005, I have to load a treeview control while loading the form so make the form loading more effective therefore I use Background worker to populate treeview. I tried treeview to pass by ref and faced the error “Cross thread operation not valid: Control” accessed from a thread other than the thread it was created on..”, which makes sense.
Now I want to load treeview in background and return its object then assigning it : Is this the right mechanism?
Can anybody give any idea about this?
Thanks in Advance
 
Create the nodes in background, assign them in UI thread. Example DoWork:
VB.NET:
Dim rootnodes As New List(Of TreeNode)
Dim node As New TreeNode("root 1")
node.Nodes.Add("sub 1")
rootnodes.Add(node)
node = New TreeNode("root 2")
node.Nodes.Add("sub 2")
rootnodes.Add(node)
e.Result = rootnodes.ToArray
example RunWorkerCompleted:
VB.NET:
Me.TreeView1.BeginUpdate()
Me.TreeView1.Nodes.AddRange(CType(e.Result, TreeNode()))
Me.TreeView1.EndUpdate()
 
Back
Top