S
H
A
R
E

Wednesday, June 01, 2011

VBnet Invoke Listbox Threading in Form

Imports System.Threading
Public Class Form1
    Delegate Sub SetText(ByVal tx As String)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t As New Thread(AddressOf AddList)
        t.Start()
    End Sub

    Sub AddList()
        For i = 0 To 10
            Addl(i & ": Set By Other Thread")
            System.Threading.Thread.Sleep(500)
        Next i
    End Sub

    ' to understand the rekursif invoke, uncomment the msgbox command
    Sub Addl(ByVal tx As String)
        If Me.InvokeRequired Then
            'MsgBox("invoke " & tx)
            Dim d As New SetText(AddressOf Addl)
            Me.Invoke(d, New Object() {tx}) ' Rekursif Invoke !!!
        Else
            'MsgBox("Set Text " & tx)
            Me.TextBox1.Text = tx
            ListBox1.Items.Add(tx)
        End If

    End Sub
End Class

Download Project Here

0 comments:

Post a Comment