Solved

Add items to a list box from a seperate thread

Posted on 2013-05-14
3
317 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

24 Experts available now in Live!

Get 1:1 Help Now