开发者

Adding text to a list view from threads

开发者 https://www.devze.com 2023-03-14 01:39 出处:网络
I am new to VB and I am trying to add text from a thread to a listview in my Form1. I have tried implementing the invokerequired method but still the new text is not added to my listview.

I am new to VB and I am trying to add text from a thread to a listview in my Form1.

I have tried implementing the invokerequired method but still the new text is not added to my listview.

(please see function addlvDataItem)

This what I call in my thread class:

Private Sub DoServerListening()
    'Thread to listen for new incoming socket clients
    Dim mSocket As System.Net.Sockets.Socket
    Dim newConnectionThread As clsTCPConnection
    Dim strRemoteIPAddress As String

    Do
        Try
            While bServerRunning = True
                If mTCPListener.Pending = True Then

                    mSocket = mTCPListener.AcceptSocket()
                    'mSocket.Blocking = True
                    If mSocket.Connected Then
                        strRemoteIPAddress = Split(mSocket.RemoteEndPoint.ToString, ":")(0)
                        newConnectionThread = New clsTCPConnection(mSocket, strRemoteIPAddress)
                        'Start the thread to handle this connection
                        Form1.addlvDataItem("Connected to " & strRemoteIPAddress.ToString(), 0)
                        Dim myThread As New System.Threading.Thread(AddressOf newConnectionThread.HandleConnection)
                        myThread.Start()
                    End 开发者_StackOverflow中文版If
                End If

            End While
        Catch ex As Exception
            If bServerRunning = True Then
                'notify main application

            End If
        End Try

    Loop

End Sub

and this is what i do in my Form1 class

Public Delegate Sub addlvDataItemCallback(ByVal [text] As String, ByVal Num As Integer)

Public Sub addlvDataItem(ByVal [text] As String, ByVal Type As Integer)
    CurrentDateTime = DateTime.Now

    If lvData.InvokeRequired Then
        Dim d As New addlvDataItemCallback(AddressOf addlvDataItem)
        Me.Invoke(d, New Object() {[text]})
    Else
        If Type = 1 Then 'TX
            Me.lvData.Items.Add("TX (" + text.Length.ToString() + " bytes): " + CurrentDateTime + " : <Start> " + text.ToString() + "<End>")
        ElseIf Type = 2 Then
            Me.lvData.Items.Add("RX (" + text.Length.ToString() + " bytes): " + CurrentDateTime + " : <Start> " + text.ToString() + "<End>")
        Else
            Me.lvData.Items.Add("Info: " + CurrentDateTime + " : " + text.ToString())

        End If
    End If
End Sub

The new text that I add is not displayed in the listview. I do not get any compile or runtime errors but still no new text to list box. I can add text from my Form1 class but not from the thread.


You provided code does throw an TargetParameterCountException. You have to pass all parameters to your delegate sub:

Me.Invoke(d, New Object() {[text], Type})

instead of

Me.Invoke(d, New Object() {[text]})
0

精彩评论

暂无评论...
验证码 换一张
取 消