Delay in Adding Item To Listbox

curlydog

Well-known member
Joined
Jul 5, 2010
Messages
50
Programming Experience
Beginner
Folks,
I have a form which contains a listbox, which I'm using to display a running log to the user.

The application retrieves data from a database and then processes it. What should happen is when the user clicks on the button to set the application running I add an item to the listbox stating that data is being fetched from the database. When the data is retrieved, I add another item to the listbox to state the data has been retrieved and is now processing.

What is actually happening is that when the user clicks the button, the listbox remains empty. The query can take some time to run (several seconds). Once the data is retrieved, both messages appear in the listbox at once. The messages are timed and I can see there are several seconds between them, even though they appear together.

I'm trying to delay the query, until the first message is displayed, but can't seem to find an event for an item being added to the listbox which I could use to trigger the query.

Any ideas how I can solve this?
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
The reason for this is that you're doing everything in serial on the UI thread. You add the first item to the ListBox and then you retrieve the data from the database on the same UI thread so that thread is never free to update the UI and display the item until the whole process finishes. You have two choices:

1. Force the UI to update before retrieving the data.
2. Retrieve the data on a secondary thread.

The first option is the easier of the two. You can either call Refresh on the ListBox or the form to force a repaint or you can call Application.DoEvents, which processes all queued events.

The second option is not all that difficult though, especially in VB2015. There are a number of options for parallelism but, as you're using Windows Forms, the first that comes to mind is to use a BackgroundWorker. You would retrieve the data in the DoWork event handler and then update the UI in the RunWorkerCompleted event handler.
 

curlydog

Well-known member
Joined
Jul 5, 2010
Messages
50
Programming Experience
Beginner
The reason for this is that you're doing everything in serial on the UI thread. You add the first item to the ListBox and then you retrieve the data from the database on the same UI thread so that thread is never free to update the UI and display the item until the whole process finishes. You have two choices:

1. Force the UI to update before retrieving the data.
2. Retrieve the data on a secondary thread.

The first option is the easier of the two. You can either call Refresh on the ListBox or the form to force a repaint or you can call Application.DoEvents, which processes all queued events.

The second option is not all that difficult though, especially in VB2015. There are a number of options for parallelism but, as you're using Windows Forms, the first that comes to mind is to use a BackgroundWorker. You would retrieve the data in the DoWork event handler and then update the UI in the RunWorkerCompleted event handler.

Thanks very much. Only a small application, first option worked a charm (refresh listbox).
 
Top Bottom