thread worker calling object in another thread

Posted on 2008-11-04
Last Modified: 2012-05-05
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

Question by:tonelm54
    LVL 85

    Accepted Solution

    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.

    Author Comment

    That is the simplest and best solution ever, Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now