Controls Created on One Thread Cannot Be Parented to a Control on a Different Thread

  • Thread starter Thread starter William Ryan
  • Start date Start date
W

William Ryan

I have the following Class but when I run this snippet

I get the above exception. BTW, this is the only line in NotifyFill - DataGrid1.DataSource = dt
So I thought I removed all control references (Originally I was passing in the Grid as a property)

I'm new to threading, so please, any help/suggestions etc is greatly appreciated.
Begin Snippet<
Try


Dim thdFill As Thread

Dim clsFiller As New AsynchGrid

AddHandler clsFiller.FillComplete, AddressOf NotifyFill

DataGrid1.Refresh()

SyncLock (DataGrid1)

DataGrid1.CaptionText = _

"Customers Grid - Thread 1"

End SyncLock





clsFiller.CustDataAdapter = SqlDataAdapter1

clsFiller.CustDataSet = dt

Dim cmd As New SqlCommand("SELECT * FROM vw_Log_Attempts", cn)

SqlDataAdapter1.SelectCommand = cmd



'clsFiller.CustDataGrid = DataGrid1

'FillCustomers() ' We remove the direct method call

' line and replace it with a thread delegate

Dim tsFill As ThreadStart = _

New ThreadStart _

(AddressOf clsFiller.FillCustomers)

thdFill = New Thread(tsFill)

thdFill.Name = "Filler Thread"

thdFill.Start()>





Imports System.Data.SqlClient

Public Class Filler

Private m_dsCustomer As DataSet

Private m_daCustomer As SqlDataAdapter

Private m_dgCustomer As DataGrid

Public Event FillComplete(ByVal intRecordsReturned As Integer)

Public Sub FillCustomers()

SyncLock (m_dgCustomer)

m_dgCustomer.CaptionText = _

"Customers Grid - Thread 2"

End SyncLock

Try

m_dsCustomer.Clear()

m_dgCustomer.DataSource = m_dsCustomer

m_daCustomer.Fill( _

m_dsCustomer)

RaiseEvent FillComplete(m_dsCustomer.Tables(0).Rows.Count)

Catch excFill As SqlClient.SqlException

Console.WriteLine(excFill.Message)

Catch excGeneral As System.Exception

Console.WriteLine(excGeneral.Message)

End Try

End Sub

Public Property CustDataSet() As DataSet

Get

CustDataSet = m_dsCustomer

End Get

Set(ByVal dsInput As DataSet)

m_dsCustomer = dsInput

End Set

End Property

Public Property CustDataAdapter() As SqlDataAdapter

Get

CustDataAdapter() = m_daCustomer

End Get

Set(ByVal daInput As SqlDataAdapter)

m_daCustomer = daInput

End Set

End Property

Public Property CustDataGrid() As DataGrid

Get

CustDataGrid = m_dgCustomer

End Get

Set(ByVal dgInput As DataGrid)

m_dgCustomer = dgInput

End Set

End Property

End Class
 
Back
Top