By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Populate TreeView in Background worker

P: 28
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
Jun 11 '10 #1
Share this Question
Share on Google+
2 Replies

Aimee Bailey
Expert 100+
P: 197
Are you creating the TreeView object within the background worker, or within your form? If it's created within a seporate thread, id imagine you'll have no end of problems as i believe winforms needs everything within one thread.

A solution is to create the worker yourself, heres a very simple example:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  3.     Dim results As TreeNode
  5.     Private Sub Button1_Click(ByVal sender As System.Object, _
  6.                               ByVal e As System.EventArgs) _
  7.                               Handles Button1.Click
  9.         Dim myWorker As New Threading.Thread(AddressOf subA)
  10.         myWorker.Start("noodle")
  11.         While myWorker.IsAlive
  12.             Application.DoEvents()
  13.         End While
  15.         If Not IsNothing(results) Then
  16.             TreeView1.Nodes.Add(results)
  17.         End If
  19.     End Sub
  21.     Sub subA(ByVal word As String)
  22.         results = New TreeNode("Worked!")
  23.         For i = 0 To 1000
  24.             results.Nodes.Add(word)
  25.         Next
  26.     End Sub
  28. End Class
Hope this helps :)

Jun 16 '10 #2

P: 28
This is usefull
Thanks AmzBee... :)
Jun 30 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.