I have multiple clients and a single server, which communicate object updates via TCP.
Basically the server runs an algorithm which accesses objects but these may be subject to change on upon an update send via TCP.
I have set flags to check for updates against the database and only send updates if there is a difference in data between the local memory or database server. The problem is if a change happens on an object while it is iterating.
The algorithm is run off a background thread and the code architecture summarised looks like the following, it is event driven:
Algorithm background thread:
Private algorithm As New NewAlgorithmTest
Private Sub bg_workerAlgorithm_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles bg_workerAlgorithm.DoWork
algorithm.RunAlgorithm()
End Sub
Private Sub bg_workerAlgorithm_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bg_workerAlgorithm.RunWorkerCompleted
bg_workerAlgorithm.RunWorkerAsync()
End Sub
Tcp Message Recieved From Client:
Private Sub Client_MessageReceived(sender As Object, e As TcpClient.MessageRecievedEventArgs)
Dim message As String = e.Message
'figure out which object to update based off message and set it as updatedObject
dim updatedObject
updatedObject.GetLatest()
End Sub
How do I go about a solution to this problem?
If you can’t block the thread until the iteration finishes, then I would implement a Stack<> (FIFO based) of the messages that are received.
This will hold the messages as they come in. You can then fire off the background thread on each of these messages in turn ensuring that there is only ever one update that has fired off an iteration on your Db.
Si