F
fniles
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.RemoveAt(0)
?
Thank you.
Public Class ListenerClass
ClientSession = New SessionClass
' Create a new worker thread to handle the client session
ClientThread = New Threading.Thread(AddressOf ClientSession.ThreadMain)
end class
Public Class frmQuoteServer
Public Function RegisterClient(ByRef Session As SessionClass, ByRef Thread
As Threading.Thread) As Boolean
ReDim Preserve Client(LastClient)
client(LastClient).Session = Session
Client(LastClient).Thread = Thread
end Function
Sub ProcessEachMessage
'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.NewQuote(sStr)
next
end sub
end Class
Public Class SessionClass
Public Server As frmQuoteServer
Private Quotes As New ArrayList
Private QuotesSync As ArrayList = ArrayList.Synchronized(Quotes)
Sub NewQuote(ByVal Message As String)
'this sub is called a receiving data thread receives data
If QuotesSync.Count > iMaxQueueDepth Then QuotesSync.Clear() 'clear the
queue if it is longer than iMaxQueueDepth
QuotesSync.Add(Message)
end Sub
Public Sub ThreadMain()
If Server.RegisterClient(Me, Thread.CurrentThread()) = 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.Count > 0 And Not (Terminated)
sPacket = QuotesSync(0)
nResult = Socket.Write(sPacket)
QuotesSync.RemoveAt(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.RemoveAt(0)
?
Thank you.
Public Class ListenerClass
ClientSession = New SessionClass
' Create a new worker thread to handle the client session
ClientThread = New Threading.Thread(AddressOf ClientSession.ThreadMain)
end class
Public Class frmQuoteServer
Public Function RegisterClient(ByRef Session As SessionClass, ByRef Thread
As Threading.Thread) As Boolean
ReDim Preserve Client(LastClient)
client(LastClient).Session = Session
Client(LastClient).Thread = Thread
end Function
Sub ProcessEachMessage
'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.NewQuote(sStr)
next
end sub
end Class
Public Class SessionClass
Public Server As frmQuoteServer
Private Quotes As New ArrayList
Private QuotesSync As ArrayList = ArrayList.Synchronized(Quotes)
Sub NewQuote(ByVal Message As String)
'this sub is called a receiving data thread receives data
If QuotesSync.Count > iMaxQueueDepth Then QuotesSync.Clear() 'clear the
queue if it is longer than iMaxQueueDepth
QuotesSync.Add(Message)
end Sub
Public Sub ThreadMain()
If Server.RegisterClient(Me, Thread.CurrentThread()) = 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.Count > 0 And Not (Terminated)
sPacket = QuotesSync(0)
nResult = Socket.Write(sPacket)
QuotesSync.RemoveAt(0)
End While
Thread.Sleep(1)
Loop
end Sub
end class