Problems with a callback  
Author Message
JuKiM





PostPosted: .NET Framework Networking and Communication, Problems with a callback Top

My problem is that using sockets to send and receive Strings.. I have the Connect Sub that calls
Clientsocket.BeginConnect(EP, AddressOf ConnectCallback, Nothing)
and the ConnectCallback calls
Clientsocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, _
AddressOf ReceiveCallBack, bytes)

And ReceiveCallback, before calling himself to receive another String, calls a delegate
Dim dlg As New OneStringDelegate(AddressOf DisplayReceivedData)

And DisplayReceivedData calls a function in a Form that tries to add the string to a ListBox, but.. The string doesn't appear in the list. If in that Function I do a MsgBox(data) it shows it, but if I do ListBox1.Items.Add(data) it doesn't. Why is this happening

Thanks!



.NET Development17  
 
 
Andreas Johansson





PostPosted: .NET Framework Networking and Communication, Problems with a callback Top

What you describes seems like you are processing the received data on the same thread as the UI. This will lead to an unresponsive UI and updates to controls will not show until the UI has had time to process the messages waiting.

Does the data appear in the listbox when it has finished to receive data



 
 
JuKiM





PostPosted: .NET Framework Networking and Communication, Problems with a callback Top

No, no data appears when it's finished the receiving. And yes, I process the received data "in the middle" of receiving... Because before than all the receiving thread ends, I call another function that works with this data...


 
 
JuKiM





PostPosted: .NET Framework Networking and Communication, Problems with a callback Top

Public Sub ReceiveCallBack(ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numbytes As Int32 = Clientsocket.EndReceive(ar)

If numbytes = 0 Then
Clientsocket.Shutdown(SocketShutdown.Both)
Clientsocket.Close()
Dim dlg As New NoParamsDelegate(AddressOf disconnectedUI)
dlg.Invoke()
Else
Dim recv As String = ASCII.GetString(bytes, 0, numbytes)
'-- Netegem Buffer
Array.Clear(bytes, 0, bytes.Length)
'-- Mostrem resultat
Dim dlg As New OneStringDelegate(AddressOf DisplayReceivedData)
dlg.Invoke(recv)
'-- Tornem a rebre
Clientsocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, _
AddressOf ReceiveCallBack, bytes)
End If
End Sub



Public Sub DisplayReceivedData(ByVal data As String)
Form1.TextBox1.Text = data 'The data should be displayed in a textbox
End Sub


And I repeat, with this, never appear the data in the textbox... Why How can I do it


 
 
Mike Flasko





PostPosted: .NET Framework Networking and Communication, Problems with a callback Top

You are likely trying to update a UI element from a thread other than the one that created the UI element. To update a UI element you MUST do it from the thread that created the UI element.

Please see my response here for more details:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=277092&SiteID=1