Tom Sage
asked on
.NET - DataGridView - loading from backgroundWorker
I want to load a DataGridView control using the BackgroundWorker so the loading will not cause a problem with the UI.
Here is some code that does work, but I am not sure if it is the best way to do this.
What are your suggestions?
Thank you
Here is some code that does work, but I am not sure if it is the best way to do this.
What are your suggestions?
Thank you
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Show()
Application.DoEvents()
'
bgWorker.RunWorkerAsync("Select CustomerID, Customername from Customer where CustomerId = '100'")
Do While bgWorker.IsBusy
Application.DoEvents()
System.Threading.Thread.Sleep(250)
Loop
'
End Sub
Private Sub bgWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
Dim dtout As New DataTable
Using con As New SqlConnection(My.Settings.DBConnectionString)
Using da As New SqlDataAdapter(e.Argument.ToString(), con)
con.Open()
da.Fill(dtout)
End Using
End Using
'
e.Result = dtout
bgWorker.CancelAsync()
End Sub
Private Sub bgWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
Dim dttemp As DataTable = CType(e.Result, DataTable)
dgv.DataSource = dttemp
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You are using a backgroundworker to load the grid and then you are making the current thread sleep in a loop while the bgw is running? That is worst than loading the grid without the bgw!
ASKER
Perhaps you could suggest a better method?
What is your aim? May be using the Shown event instead of Load event would suffice? Why do you want the UI to be responsive? Is there anything else on the UI the user would be interested in while the grid is loading?
ASKER
Besides loading the Grid, there are some graphs I am updating based on user selection where I get values from SQL and create a chart using Excel that is displayed in a picture box.
Just seems like it is more user friendly to have the Form moveable while it is being loaded rather than frozen. When the Form is not moveable by the mouse, it seems like the program has stopped.
Thanks
Just seems like it is more user friendly to have the Form moveable while it is being loaded rather than frozen. When the Form is not moveable by the mouse, it seems like the program has stopped.
Thanks
What happens if you remove this code
Do While bgWorker.IsBusy
Application.DoEvents()
System.Threading.Thread.Sl eep(250)
Loop
Do While bgWorker.IsBusy
Application.DoEvents()
System.Threading.Thread.Sl
Loop
you should really have a look at the asynchronous features of ADO.Net
ASKER
CodeCruiser - If remove that code the program will stop without waiting for the backgroundWorker to complete.
Emoreau - I did download your sample code. I will give that a try. it may be tomorrow before I have a chance to test it.
Thanks
Emoreau - I did download your sample code. I will give that a try. it may be tomorrow before I have a chance to test it.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you