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

x
?
Solved

thread worker calling object in another thread

Posted on 2008-11-04
2
Medium Priority
?
153 Views
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"
 
        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

0
Comment
Question by:tonelm54
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 22880133
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
 

Author Comment

by:tonelm54
ID: 22880339
That is the simplest and best solution ever, Thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

571 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