Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

.NET - DataGridView - loading from backgroundWorker

Posted on 2011-05-10
10
Medium Priority
?
437 Views
Last Modified: 2012-05-11
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

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

Open in new window

0
Comment
Question by:Tom Sage
  • 4
  • 4
  • 2
10 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 35730744
you better use the asynchronous features of ADO.Net: http://www.emoreau.com/Entries/Articles/2006/11/MARS-and-Asynchronous-ADONet.aspx
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35731666
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!
0
 

Author Comment

by:Tom Sage
ID: 35731687
Perhaps you could suggest a better method?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35731731
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?
0
 

Author Comment

by:Tom Sage
ID: 35731795
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35731942
What happens if you remove this code

Do While bgWorker.IsBusy
            Application.DoEvents()
            System.Threading.Thread.Sleep(250)
        Loop
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35731963
you should really have a look at the asynchronous features of ADO.Net
0
 

Author Comment

by:Tom Sage
ID: 35731996
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
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 35732021
> the program will stop without waiting for the backgroundWorker to complete.

It wont. The UI will be more responsive and when the bgw finishes, it will display the results.
0
 

Author Closing Comment

by:Tom Sage
ID: 35732782
Thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

581 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