[vb.net] resolves that a control created on a thread cannot become the parent of a control created on another thread

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.