How to Use Form Controls in the. NET Programming thread

Source: Internet
Author: User

When writing VB.net, it is not that easy to manually change the information in the listview control on the form in the thread. Because Microsoft's framework does not encourage direct access to controls on the form in the thread. This will change the normal execution of the UI process. If this is required, you need to use the invoke function or the invokebegin function in the thread. The following is an example:

Imports system. threading

Public class form1
Delegate sub addlistitem (byval ipstring as string, byval scanport as integer)
Public mydelegate as addlistitem
Public openportcount as integer = 0

Private sub form1_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
Mydelegate = new addlistitem (addressof addlistitemmethod)
End sub

Private sub start_button_click (byval sender as system. Object, byval e as system. eventargs) handles start_button.click
Dim mythread as thread
Mythread = new thread (New threadstart (addressof threadfunction ))
Mythread. Start ()

End sub

Private sub threadfunction ()
Dim mythread as thread
Mythread = new thread (New threadstart (addressof doscanthread ))
Mythread. Start ()
End sub 'threadfunction

Private sub doscanthread ()
Dim mythreadclassobject as new scanthreadclass (me)
Mythreadclassobject. Run ()
End sub

Public sub addlistitemmethod (byval ipstring as string, byval scanport as integer)
Listview_result.items.add (ipstring, openportcount) 'scanip. tostring (), 0)
Listview_result.items (openportcount). subitems. Add (scanport. tostring ())
Openportcount + = 1
End sub 'addlistitemmethod

End Class

Public class scanthreadclass
Private myformcontrol1 as form1

Public sub new (byval myform as form1)
Myformcontrol1 = myform
End sub 'new

Public sub run ()
Myformcontrol1.invoke (myformcontrol1.mydelegate, new object () {"11", 123 })
End sub

End Class

The main note is the call of invoke and the definition of delegate.

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.