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"
 
        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)
                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")
        Finally
            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
        BackgroundWorker1.RunWorkerAsync()
    End Sub
End Class

Open in new window

tonelm54Asked:
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 TomlinsonMiddle School Assistant TeacherCommented:
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.
0

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.