tony obrien <to**@sourcecod e-inc.comwrote in
news:Xn******** *************** **********@130. 81.64.196:
Jon Skeet [C# MVP] <sk***@pobox.co mwrote in
news:MP******** *************** *@msnews.micros oft.com:
Hi Jon, (I very much appreciate your taking the time and energy to help
me thru this !)
I have been able to at least determine how the problem exhibits itself...
the very bottom has this explanation.
Below are some snippets to give you an idea of how this load tester
client operates....(an d yes I have left out gobs of code as to not bore
(overwhelm !) you... but remember this all works as a single instance so
I propose that it is in the 'multithreaded-ness' of it.)
------- IN MAIN:
'Create a new instance of the tcp interface class
aSession = New serviceConnecti on
'create a new thread
serverListenerT hread = New Threading.Threa d(AddressOf
aSession.startT hisThread)
'I then start the thread....
serverListenerT hread.Start()
------ IN THE SERVICECONNECTI ON CLASS:
=============== =============== =======
Private clientService as TCPClient
Private Event OnLineReceived( ByVal sender As serviceConnecti on, ByVal
Data() As Byte, ByVal theBytesReadCou nt As Integer)
=============== =============== =======
Public Sub StartThisThread ()
' Create an event handler to allow this serviceconnecti on class to
communicate
AddHandler OnLineReceived, AddressOf OnLineReceivedS ervice
Try
clientService = New TcpClient(mySer verNameStr$,
mSessionPortNum ber)
clientService.G etStream.BeginR ead(readBuffer, 0,
READ_BUFFER_SIZ E, AddressOf ServiceStreamRe ceiver, Nothing)
SOM = True
Catch Ex As Exception
mlastErrorStrin g = Ex.Message
Exit Sub
End Try
'Now I sit in a loop asking the SERVER for various services
=============== =============== ========
Private Sub ServiceStreamRe ceiver(ByVal ar As IAsyncResult)
Try
' Ensure that no other threads try to use the stream at the
same time.
SyncLock client.GetStrea m
' Finish asynchronous read into readBuffer and get number
of bytes read.
LCBytesRead = client.GetStrea m.EndRead(ar)
End SyncLock
RaiseEvent OnLineReceived( Me, readBuffer, LCBytesRead)
' Ensure that no other threads try to use the stream at the
same time.
SyncLock client.GetStrea m
' Start a new asynchronous read into readBuffer.
LCBytesRead = 0
client.GetStrea m.BeginRead(rea dBuffer, 0,
READ_BUFFER_SIZ E, AddressOf ServiceStreamRe ceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub
=============== =============== ========
Private Sub OnLineReceivedS ervice(ByVal thisServiceConn ection As
serviceConnecti on, ByVal data() As Byte, ByVal theBytesReadCou nt As
Integer)
'This is the EVENT raised by ServiceStreamRe ceiver above....
'It gets passed the "instance" of the connection, the bytecount and
data... and this routine operates on the data.
=============== =============== ========
=============== =============== ========
=============== =============== ========
OK, so what happens is that when I start the 2nd or 3rd thread the
OnLineReceivedS ervice callback will get passed the "wrong instance" on
some response message that was received (recall that there are supposedly
distinct "thisServiceCon nection" classes created and "started".. .) and so
some instance is waiting for a response it never gets... and the instance
that does get passed throws the message away because it wasn't meant for
him.
This implies that the "wrong" instance of ServiceStreamHa ndler is being
called... perhaps the TCP.net stuff I am using does not allow me to do
what I am trying to do?
regards,
tob