Problems with using form controls on a thread during. NET programming

Source: Internet
Author: User
When writing vb.net, it is not so easy to find the information in the ListView control that you want to thread on the form to be changed online. Because Microsoft's framework does not encourage online thread to directly access controls on the form. Because this will change the normal execution of the UI process. If you must do this, you need to use the Invoke function or the Invokebegin function in the thread. Here 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 thing to note is the invocation of invoke and the definition of delegate.

  • Related Article

    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.