473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Populate TreeView in Background worker

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
2 4124
Aimee Bailey
197 Expert 100+
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
  2.  
  3.     Dim results As TreeNode
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, _
  6.                               ByVal e As System.EventArgs) _
  7.                               Handles Button1.Click
  8.  
  9.         Dim myWorker As New Threading.Thread(AddressOf subA)
  10.         myWorker.Start("noodle")
  11.         While myWorker.IsAlive
  12.             Application.DoEvents()
  13.         End While
  14.  
  15.         If Not IsNothing(results) Then
  16.             TreeView1.Nodes.Add(results)
  17.         End If
  18.  
  19.     End Sub
  20.  
  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
  27.  
  28. End Class
  29.  
  30.  
Hope this helps :)


Aimee.
Jun 16 '10 #2
This is usefull
Thanks AmzBee... :)
Jun 30 '10 #3

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

Similar topics

1
by: Robert Robinson | last post by:
I have been working for the past week on what seems to be a bug with the VB TreeView control. I have changed the background of the treeview to a light beige, this works fine except for the white...
1
by: bilosta | last post by:
I need help with background worker and form I have a class (form1) that works with database. I need this: -Show form2 while form1 is geting data from database -When form1 is done, close form2,...
6
by: sjoshi | last post by:
I'm able to use the Bacground Worker class to execute task and pain the UI. However I want to be able to execute n tasks in order, say n1, then n2 if there were no errors during n1, n3 so on. ...
4
by: Jesse Aufiero | last post by:
I'm wondering what the rule is on how to assure that the background worker 'DoWork' routine is truly running entirely in the background. My DoWork routine calls another procedure which resides in...
0
by: twoSeven | last post by:
hi, Here is what i want to do. I am making a file and folder browser similar to Windows Explorer with a treeview and listview.. In that, i get icons and thumbnails of a particular file/folder from...
4
by: csharpula csharp | last post by:
Hello, Can you please tell me if I should use the background worker in the following way: if (worker.IsBusy) worker.CancelAsync(); (or I can avoid this if)? And in case I want to restart the...
7
by: csharpula csharp | last post by:
Hello, I got a question regarding the usage of background worker. How can I run few threads via background worker with different objects as parameter each time. I understood that I can't do...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
2
by: thoffman | last post by:
Hello again everyone! I have an issue that I can't seem to find a straight answer for anywhere... I have a For loop that sends a file name to a background worker that contains some long running...
1
by: Infog | last post by:
I am trying to code a simple background workers class without needing to use the backgroundworker object on a form. This would allow me to use a background worker in any class without writing much...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.