Question Fill Datatable in the Background

Rosinante

New member
Joined
Aug 16, 2011
Messages
2
Programming Experience
1-3
Hi there,

I've searched around for this but can't find any answers. Any help would be appreciated.

I would like the main form I am designing in VB.net to appear as quickly as possible. As a result I don't want to fill a datatable in the load event but instead fill it as soon as the form appears, in the background.Then when a user clicks the button a listbox can quickly be populated.

With this in mind I called a function to fill the datable from the _Shown event. The trouble is that the form freezes until the query executes, making the process pointless for my needs.

Does anyone know how to keep the form active while the datatable fills silently in the background?

I would be grateful of any pointers.
 
You need to populate the DataTable on a secondary thread. If you do it on the UI thread then the UI must freeze because the thread is too busy to refresh it. Your simplest option is to use a BackgroundWorker, which you can start from the Load event handler.
 
What happens if the form loads and the user clicks the buttom and the datatables has not finished loading?
Be sure to prevent use from activating the button until the datatable is loaded.

Solution Create a sepreate thread to run the process Asynchronously with a callback.

look into Threading, Callbacks and asynchronous processes.

sorry didn't read previous reply.
 
What happens if the form loads and the user clicks the buttom and the datatables has not finished loading?
Be sure to prevent use from activating the button until the datatable is loaded.

Solution Create a sepreate thread to run the process Asynchronously with a callback.

look into Threading, Callbacks and asynchronous processes.

sorry didn't read previous reply.
What you say is essentially true, but the BackgroundWorker wraps all that up into a simple package. You call RunWorkerAsync and the DoWork event is raised on a secondary thread. You do your background work in the DoWork event handler and then the RunWorkerAsync event is raised on the UI thread. You then update the UI in the RunWorkerCompleted event handler. The BGW has a few other tricks up its sleeve too, which I have demonstrated here:

Using the BackgroundWorker Component
 
Back
Top