473,785 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket / Threading / Callback Question

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(A ddressOf OnDataReceived)

End If

If Not bMyRun Then

Me.objMyNetwork Stream.Close()

Else

Me.m_asynResult = objMySocket.Beg inReceive(objMy Bytes, 0,
objMyBytes.Leng th, SocketFlags.Non e, Callback, Nothing)

End If

End Sub

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

Public Sub OnDataReceived( ByVal asyn As IAsyncResult)

Dim iRx As Integer = 0

Dim sLCompleteMessa ge As String

If Me.objMySocket. Connected Then

iRx = Me.objMySocket. EndReceive(asyn )

End If

If iRx > 0 Then

Dim numberOfBytesRe ad As Integer = 0

sLCompleteMessa ge =
Encoding.ASCII. GetString(objMy Bytes).Substrin g(0, iRx)

RaiseEvent OnDataReceivedE vent(sLComplete Message)

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
OnDataReceivedE vent).

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
Nov 20 '05 #1
2 1770
Might want to throw an application.DoE vents() in there.
"Marten Van Keer" <ma****@c3.be > wrote in message
news:3f******** *************** @reader3.news.s kynet.be...
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(A ddressOf OnDataReceived)

End If

If Not bMyRun Then

Me.objMyNetwork Stream.Close()

Else

Me.m_asynResult = objMySocket.Beg inReceive(objMy Bytes, 0,
objMyBytes.Leng th, SocketFlags.Non e, Callback, Nothing)

End If

End Sub

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

Public Sub OnDataReceived( ByVal asyn As IAsyncResult)

Dim iRx As Integer = 0

Dim sLCompleteMessa ge As String

If Me.objMySocket. Connected Then

iRx = Me.objMySocket. EndReceive(asyn )

End If

If iRx > 0 Then

Dim numberOfBytesRe ad As Integer = 0

sLCompleteMessa ge =
Encoding.ASCII. GetString(objMy Bytes).Substrin g(0, iRx)

RaiseEvent OnDataReceivedE vent(sLComplete Message)

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
OnDataReceivedE vent).

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

Nov 20 '05 #2
In article <3f************ ***********@rea der3.news.skyne t.be>, Marten Van Keer wrote:
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(A ddressOf OnDataReceived)

End If

If Not bMyRun Then

Me.objMyNetwork Stream.Close()

Else

Me.m_asynResult = objMySocket.Beg inReceive(objMy Bytes, 0,
objMyBytes.Leng th, SocketFlags.Non e, Callback, Nothing)

End If

End Sub

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

Public Sub OnDataReceived( ByVal asyn As IAsyncResult)

Dim iRx As Integer = 0

Dim sLCompleteMessa ge As String

If Me.objMySocket. Connected Then

iRx = Me.objMySocket. EndReceive(asyn )

End If

If iRx > 0 Then

Dim numberOfBytesRe ad As Integer = 0

sLCompleteMessa ge =
Encoding.ASCII. GetString(objMy Bytes).Substrin g(0, iRx)

RaiseEvent OnDataReceivedE vent(sLComplete Message)

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
OnDataReceivedE vent).

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.
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3284
by: Gonçalo Rodrigues | last post by:
Hi, My setup is the following: I have socket s from which I want to read and write. So I made the following set up: There is a thread whose only job is to read. Any data read (from recv call) is just passed to (some) Queue. This thread is "owned" by a second thread waiting on a Queue for write requests. The thread just pops these from the Queue, and calls the send method from the socket. This thread also takes care of closing the...
1
3398
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or stopped by calls to the appropriate functions. The server class has...
0
2062
by: Vladimir Lushnikov | last post by:
Hi, I'm experimenting with the Seqpacket protocol, because the connectionless protocols aren't enough for my needs, and TCP/IP doesn't preserve message boundaries. This is what I've currently got in C# -------------------------------------------------------------------------- listeningSock = new Socket(AddressFamily.InterNetwork, SocketType.Seqpacket, ProtocolType.Unknown);
2
702
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
0
2716
by: John J. Hughes II | last post by:
In my code I have the below callback. The socket I am connected to is dropped by the other end it would be nice to not set off the exception. As you can see from the code I check if r is null and if the async state is null but I am still getting the below error. Is there a better way of handling this? Regards, John
9
3604
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
2
6879
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server" app. I am using the standard asynch socket code from MSDN to listen for connections and they...
5
4771
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a set of sensor data per second to a socket server. The socket server will do two things: 1. write data into a file via bsddb; 2. forward the data to a GUI written in wxpython. I am thinking the code should work as follow (not sure it is feasible)...
14
5944
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still connected. Any idea? Thanks.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.