In the code that binds a DataTable object to the DataGrid control,
When you join a thread, you are prompted that "a control created on a thread cannot become the parent of a control created on another thread"
Find other people's advice in CSDN forums "Use the delegate method to get control of the control, and then manipulate the control."
The idea has, is the implementation.
The first step is to declare delegates and instance-session delegates
Private Delegate Sub binddatagriddelegate () creates delegates and delegate objects
Private mybinddatagriddelegate as Binddatagriddelegate = New binddatagriddelegate (AddressOf selectdata) ' Selectdata is included The data-bound code snippet for the DataGrid
Step two, get ready to join the thread
Private Bindgridthread as Thread
Private Sub Invokebinddatagrid () '//calls to controls that are not created by this thread must be invoked or BeginInvoke. Otherwise, an exception will be thrown
Dg_ysinfo.invoke (mybinddatagriddelegate, nothing)
End Sub
Private Sub Stopbindthread ()
If not bindgridthread are nothing Then
If bindgridthread.isalive Then
Bindgridthread.abort ()
Bindgridthread.join ()
End If
End If
Bindgridthread = Nothing
End Sub
The third step, in the button click event, invokes the thread to bind the query result to the DataGrid
Private Sub Btrun_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btrun.click
Stopbindthread ()
Bindgridthread = new Thread (new ThreadStart (AddressOf invokebinddatagrid)) '; Call by delegate, legal
Bindgridthread.start ()
End Sub
With the above 3 steps, the final "control created on one thread cannot be the parent of a control created on another thread" is resolved.
The result of the test is what you expect.