Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Hi,

This is my code:

VB
Private Delegate Sub UpdateListInvoker(ByVal WhatToWrite As String)
  Private Sub UpdateListBox(ByVal WhatToWrite As String)
      If Form1.ListBox2.InvokeRequired Then
          Form1.ListBox2.Invoke(New UpdateListInvoker(AddressOf UpdateListBox), WhatToWrite)
      Else
          Form1.ListBox2.Items.Add(WhatToWrite)
      End If
  End Sub


To use: UpdateListBox("Hello")

It just isn't doing anything. Why?

-Rixterz
Posted
Updated 15-Jun-14 7:01am
v2
Comments
OriginalGriff 12-Jun-14 12:31pm    
Are you calling it?
What happens in the debugger?
[no name] 12-Jun-14 12:35pm    
When the form loads, there is Receiver.RunWorkerAsync(), where Receiver is a backgroundworker.
This is Receiver's DoWork sub:

Private Sub Receiver_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles Receiver.DoWork
Try
Dim TCPListener As New TcpListener(IncomingPort)
TCPListener.Start()
While True
Dim client As New ClientConnection(TCPListener.AcceptTcpClient)
End While
Catch ex As Exception

End Try
End Sub

And in the ClientConnection class, this code calls the update:

Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
Dim bytesRead As Integer
Try
SyncLock TCPClient.GetStream
bytesRead = TCPClient.GetStream.EndRead(ar)
End SyncLock
If bytesRead < 1 Then
AllClients.Remove(ClientIP)
UpdateListBox("Disconnected")
Exit Sub
Else
Dim MessageReceived As String = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead)
If MessageReceived.StartsWith("PCDetails") Then
Dim MachineNameAndUserName As String = MessageReceived.Split(":")(1)
UpdateListBox("Details received: " & MachineNameAndUserName)
End If
End If
SyncLock TCPClient.GetStream
TCPClient.GetStream.BeginRead(data, 0, CInt(TCPClient.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
End SyncLock
Catch ex As Exception
AllClients.Remove(ClientIP)
UpdateListBox("Disconnected")
End Try
End Sub
Duncan Edwards Jones 16-Jun-14 8:01am    
This loop :-

While True
Dim client As New ClientConnection(TCPListener.AcceptTcpClient)
End While

Doesn't seem to do anything with the received client connection. Is there something in the constructor that does or..?
[no name] 16-Jun-14 14:57pm    
It calls the New() sub in the class ClientConnection. The communications are fine, but the listbox won't update when a message is received.
[no name] 12-Jun-14 12:39pm    
I forgot to say that nothing shows up in the debugger.

1 solution

Ok, so it looks like your trying to invoke a delegate on form1 from a different form in your project, and when I tested THAT (as opposed to a thread started from form1), I had the same problem you did.

You need to make a refrence to form1 available to the background thread (or from form2). This is how I got it to work (calling a delegate on form1 from form2) in my quick test app:

Try this:

VB
' Form1 code:

' Define your delegate:
Public Delegate Sub UpdateListInvoker(ByVal whatToWrite As String)

' Create your reference object:
Public UpdateListBox As UpdateListInvoker

' Your callback:
Public Sub ListBoxUpdater(ByVal WhatToWrite As String)
   If Me.InvokeRequired Then
        Me.Invoke(UpdateListBox, WhatToWrite)
   Else
        Me.ListBox2.Items.Add(WhatToWrite)
    End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    UpdateListBox = AddressOf ListBoxUpdater
    Form2.f1 = Me
    Form2.Show()
End Sub


' Form2 code:
Public f1 As Form1

Private Sub bgThread()
    ' Elsewhere in your code...
    f1.UpdateListBox("what you're adding to the listbox")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim t As New Threading.Thread(AddressOf bgThread)
    t.Start()
End Sub


You see that in form2, I'm starting a background thread when I click a button, and then from the background thread I'm updating a listbox on form1 by using it's reference (f1).

This should do the trick for you.

-Pete
 
Share this answer
 
v9
Comments
[no name] 17-Jun-14 14:07pm    
Nope. I recorded the application in action here: https://hostr.co/aTp4uOx2tZgh
pdoxtader 18-Jun-14 7:41am    
Ok, well this IS the correct way to use delegates. I've done it many times. I'm going to put together a quick and dirty test app to verify that I haven't forgotten anything here... but again: this IS the correct way to do it. If this isn't working for you, then there's something else wrong in your code that we can't see here given what you've posted.
pdoxtader 18-Jun-14 8:17am    
Updated the solution. I think this is what you need.
[no name] 18-Jun-14 13:47pm    
I'm afraid it's not as easy as that. You'll only understand if you view my solution here: https://hostr.co/mU0XJvlbqJJ8
pdoxtader 18-Jun-14 14:30pm    
Here ya go: https://hostr.co/Z9xq2ggnIknt

Let me know if you have any trouble.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900