Solved

Add items to a list box from a seperate thread

Posted on 2013-05-14
3
316 Views
Last Modified: 2013-05-15
Hello Everyone,

I figured this would be easier than it is working out to so it has to be a case that I' just can't see the trees for the forest.

On form1 I have a picture box shows an animated gif and has a listbox that I need to populate with a list of Sql Servers.

I can retrieve the list of servers without any problem except while the process is running the picture box stops and I understand why. So I created a BackgroundWorker  to handle the routine that checks for the server by using the DoWork event. That runs fine goes through and finds the servers that it is supposed and my picture boxes are doing what they are supposed to do. Just for clarity this routine is in a completely separate module.

My question is how do I  add items to my listbox from the routine that is called in the second thread that generates the list of servers. I have tried several different ways and I get all kinds of exception and delegate errors.

Thanks,
Art
0
Comment
Question by:Nashua58
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 250 total points
ID: 39166979
in the DoWork method, set e.result = [object or value needed]

In the RunWorkerCompleted method, you can process e.result and do what you need.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 39167495
You need to use the Progress events. You can use a class or module level variable to store the text that you want to add to listbox.

Examples here

http://www.java2s.com/Code/VB/GUI/BackgroundWorkerDemo.htm
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39167916
For future readers, the first suggestion means that items are added to the list at the end of run not during the run.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now