• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Add items to a list box from a seperate thread

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.

  • 2
2 Solutions
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.
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

For future readers, the first suggestion means that items are added to the list at the end of run not during the run.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now