I am using VB.NET 2003 and a socket control to receive and sending data to
clients.
As I receive data in 1 thread, I put it into an arraylist, and then I remove
the data from arraylist and send it to the client.
Before adding data to the arraylist, I check if the depth of the arraylist
is longer than iMaxQueueDepth, and if it is, I clear the arraylist.
Is it possible that while I am clearing the arraylist, the ThreadMain at the
same time also trying to remove the data in the arraylist ?
In other words, in the following codes, in the Sub NewQuote doing
QuotesSync.Add( Message) at the same time ThreadMain doing
sPacket = QuotesSync(0)
QuotesSync.Remo veAt(0)
?
Thank you.
Public Class ListenerClass
ClientSession = New SessionClass
' Create a new worker thread to handle the client session
ClientThread = New Threading.Threa d(AddressOf ClientSession.T hreadMain)
end class
Public Class frmQuoteServer
Public Function RegisterClient( ByRef Session As SessionClass, ByRef Thread
As Threading.Threa d) As Boolean
ReDim Preserve Client(LastClie nt)
client(LastClie nt).Session = Session
Client(LastClie nt).Thread = Thread
end Function
Sub ProcessEachMess age
'This sub is called by another thread that receives quotes
For lIndex = 0 To LastClient - 1
'assigning the quote to the client session
Client(lIndex). Session.NewQuot e(sStr)
next
end sub
end Class
Public Class SessionClass
Public Server As frmQuoteServer
Private Quotes As New ArrayList
Private QuotesSync As ArrayList = ArrayList.Synch ronized(Quotes)
Sub NewQuote(ByVal Message As String)
'this sub is called a receiving data thread receives data
If QuotesSync.Coun t iMaxQueueDepth Then QuotesSync.Clea r() 'clear the
queue if it is longer than iMaxQueueDepth
QuotesSync.Add( Message)
end Sub
Public Sub ThreadMain()
If Server.Register Client(Me, Thread.CurrentT hread()) = False Then
:
Exit Sub
End If
'this client thread is looping all the time until it is terminated
Do While Not Terminated
sPacket = ""
While QuotesSync.Coun t 0 And Not (Terminated)
sPacket = QuotesSync(0)
nResult = Socket.Write(sP acket)
QuotesSync.Remo veAt(0)
End While
Thread.Sleep(1)
Loop
end Sub
end class
clients.
As I receive data in 1 thread, I put it into an arraylist, and then I remove
the data from arraylist and send it to the client.
Before adding data to the arraylist, I check if the depth of the arraylist
is longer than iMaxQueueDepth, and if it is, I clear the arraylist.
Is it possible that while I am clearing the arraylist, the ThreadMain at the
same time also trying to remove the data in the arraylist ?
In other words, in the following codes, in the Sub NewQuote doing
QuotesSync.Add( Message) at the same time ThreadMain doing
sPacket = QuotesSync(0)
QuotesSync.Remo veAt(0)
?
Thank you.
Public Class ListenerClass
ClientSession = New SessionClass
' Create a new worker thread to handle the client session
ClientThread = New Threading.Threa d(AddressOf ClientSession.T hreadMain)
end class
Public Class frmQuoteServer
Public Function RegisterClient( ByRef Session As SessionClass, ByRef Thread
As Threading.Threa d) As Boolean
ReDim Preserve Client(LastClie nt)
client(LastClie nt).Session = Session
Client(LastClie nt).Thread = Thread
end Function
Sub ProcessEachMess age
'This sub is called by another thread that receives quotes
For lIndex = 0 To LastClient - 1
'assigning the quote to the client session
Client(lIndex). Session.NewQuot e(sStr)
next
end sub
end Class
Public Class SessionClass
Public Server As frmQuoteServer
Private Quotes As New ArrayList
Private QuotesSync As ArrayList = ArrayList.Synch ronized(Quotes)
Sub NewQuote(ByVal Message As String)
'this sub is called a receiving data thread receives data
If QuotesSync.Coun t iMaxQueueDepth Then QuotesSync.Clea r() 'clear the
queue if it is longer than iMaxQueueDepth
QuotesSync.Add( Message)
end Sub
Public Sub ThreadMain()
If Server.Register Client(Me, Thread.CurrentT hread()) = False Then
:
Exit Sub
End If
'this client thread is looping all the time until it is terminated
Do While Not Terminated
sPacket = ""
While QuotesSync.Coun t 0 And Not (Terminated)
sPacket = QuotesSync(0)
nResult = Socket.Write(sP acket)
QuotesSync.Remo veAt(0)
End While
Thread.Sleep(1)
Loop
end Sub
end class
Comment