[VB.net] solves the problem of "The control created on a thread cannot be the parent level of the Control created on another thread"

Source: Internet
Author: User

In a piece of code that binds a able object to the DataGrid Control,

After a thread is added, the system prompts "The control created on a thread cannot be the parent level of the Control created on another thread"

 

In the csdn Forum, find other people's suggestion. "Use the delegate method to obtain control of the control first, and then perform operations on the control"

 

The idea is implemented.

Step 1: declare delegation and instance-based Delegation

Private delegate sub binddatagriddelegate () 'create a delegate and delegate object
Private mybinddatagriddelegate as binddatagriddelegate = new binddatagriddelegate (addressof selectdata) 'selectdata is the code segment containing the data binding of the DataGrid

Step 2: Prepare to join the thread

Private bindgridthread as thread

Private sub invokebinddatagrid () '// you must use invoke or begininvoke to call the operations of controls created by this thread. Otherwise, an exception is thrown.
Dg_ysinfo.invoke (mybinddatagriddelegate, nothing)
End sub

Private sub stopbindthread ()
If not bindgridthread is nothing then
If bindgridthread. isalive then
Bindgridthread. Abort ()
Bindgridthread. Join ()
End if
End if
Bindgridthread = nothing
End sub

Step 3: In the button clicking event, call the thread to bind the DataGrid to the query result.

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) '; // The call is valid by Delegate.
Bindgridthread. Start ()
End sub

 

After completing the preceding three steps, we finally solved the problem of "The control created on a thread cannot be the parent level of the Control created on another thread,

The test results are expected

 

 

 

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.