Socket / Threading / Callback Question

M

Marten Van Keer

Hi;



I have two applications A and B



*** Application B listens on a network stream with a callback function:



-------------------------------------------

Public Sub WaitForData()

If Callback Is Nothing Then

Callback = New AsyncCallback(AddressOf OnDataReceived)

End If

If Not bMyRun Then

Me.objMyNetworkStream.Close()

Else

Me.m_asynResult = objMySocket.BeginReceive(objMyBytes, 0,
objMyBytes.Length, SocketFlags.None, Callback, Nothing)

End If

End Sub

----------------------------------------------------------------

Public Sub OnDataReceived(ByVal asyn As IAsyncResult)

Dim iRx As Integer = 0

Dim sLCompleteMessage As String

If Me.objMySocket.Connected Then

iRx = Me.objMySocket.EndReceive(asyn)

End If

If iRx > 0 Then

Dim numberOfBytesRead As Integer = 0

sLCompleteMessage =
Encoding.ASCII.GetString(objMyBytes).Substring(0, iRx)

RaiseEvent OnDataReceivedEvent(sLCompleteMessage)

Me.WaitForData()

End If

End Sub

-----------------------------------------



The OnDataReceived function is called when... data receives on the
networkstream. As you can see an event is raised (RaiseEvent
OnDataReceivedEvent).

Application A receives this event and does a refresh of the listview.



Now my question is the following:



The listview in application A can be refreshed by pressing F5 or by
receiving the event raised by Application B.



When the listview is being refresh manually (F5-press) the refresh is very
fast.

When the listview is being refresh automatically (by receiving event of
application B) the refresh goes very slow and the screen is being repainted
very slowly.



Is there something wrong with the OnDataReceived Sub ? Is this a threading
problem? Anyone any idea why the refresh slows down when it is triggered
externally by application B ?







Hope someone can help me out.



Regards
MVK
 
T

Tom Shelton

Hi;



I have two applications A and B



*** Application B listens on a network stream with a callback function:



-------------------------------------------

Public Sub WaitForData()

If Callback Is Nothing Then

Callback = New AsyncCallback(AddressOf OnDataReceived)

End If

If Not bMyRun Then

Me.objMyNetworkStream.Close()

Else

Me.m_asynResult = objMySocket.BeginReceive(objMyBytes, 0,
objMyBytes.Length, SocketFlags.None, Callback, Nothing)

End If

End Sub

----------------------------------------------------------------

Public Sub OnDataReceived(ByVal asyn As IAsyncResult)

Dim iRx As Integer = 0

Dim sLCompleteMessage As String

If Me.objMySocket.Connected Then

iRx = Me.objMySocket.EndReceive(asyn)

End If

If iRx > 0 Then

Dim numberOfBytesRead As Integer = 0

sLCompleteMessage =
Encoding.ASCII.GetString(objMyBytes).Substring(0, iRx)

RaiseEvent OnDataReceivedEvent(sLCompleteMessage)

Me.WaitForData()

End If

End Sub

-----------------------------------------



The OnDataReceived function is called when... data receives on the
networkstream. As you can see an event is raised (RaiseEvent
OnDataReceivedEvent).

Application A receives this event and does a refresh of the listview.



Now my question is the following:



The listview in application A can be refreshed by pressing F5 or by
receiving the event raised by Application B.



When the listview is being refresh manually (F5-press) the refresh is very
fast.

When the listview is being refresh automatically (by receiving event of
application B) the refresh goes very slow and the screen is being repainted
very slowly.



Is there something wrong with the OnDataReceived Sub ? Is this a threading
problem? Anyone any idea why the refresh slows down when it is triggered
externally by application B ?







Hope someone can help me out.



Regards
MVK

These are two separate applications? I mean two separate executables?
Are you using Remoting here? Can you post the code for the refresh
function?

Just from the fact that they are two separate applications, I would
expect for things to be somewhat slower - your having to marshal data
across processes. And from the looks of it, your calling this event for
every bit of data that is sent over the socket. I'm not sure of what's
being sent, but that could mean multiple transmissions per message
depending on how the packets get broken up. Which may mean that your
stacking up events - and having to do all that cross process marshalling
could be pretty expensive. If I were you, I would not raise the event
from process B until a complete message is recieved. In other words,
buffer the data in app B until you have a complete transmission, and
then raise the event and pass in the data to app A in one chunk.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top