thread worker calling object in another thread

Got a simple proof of concept program which I want to remain responsive, thus taking the workings of the program onto another thread, which works great, until it gets to the line:-
                DataGridView1.DataSource = myData

Where I get the error message:-
Cross-thread operation not valid: Control 'DataGridView1' accessed from a thread other than the thread it was created on.

I think I understand the message, and think I need to use addressof to set datagridview1 but have no idea how to do this.

Any ideas?
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class Form1
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim conn As New MySqlConnection
        Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As New DataTable
        Dim SQL As String
        SQL = "Select * from mytable limit 0,5000;"
        conn.ConnectionString = "server=localhost;user id=root;password=;database=testdb"
                myCommand.Connection = conn
                myCommand.CommandText = SQL
                myAdapter.SelectCommand = myCommand
                DataGridView1.DataSource = myData
                DataGridView1.AllowUserToAddRows = False
                DataGridView1.AllowUserToDeleteRows = False
            Catch myerror As MySqlException
                MsgBox("Error reading")
            End Try
        Catch myerror As MySqlException
            MsgBox("Error connecting")
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
End Class

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
In your DoWork() method, set the result of the operation using:

    e.Result = myData

Then in the RunWorkerCompleted() event, set the DataSource:

    DataGridView1.DataSource = e.Result
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.AllowUserToDeleteRows = False

You need to set the BackgroundWorker controls WorkerReportsProgress() property to True.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tonelm54Author Commented:
That is the simplest and best solution ever, Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.