Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this threading code doing what I think it is?
    text
    copied!<p>I have written the following code to perform some simultaneous HTTP posting and file archiving:</p> <pre><code>Dim t1 As New Threading.Thread(New Threading.ThreadStart(AddressOf ProcessNTSMessageQueue)) Dim t2 As New Threading.Thread(New Threading.ThreadStart(AddressOf ProcessNTSMessageQueue)) Dim t3 As New Threading.Thread(New Threading.ThreadStart(AddressOf ProcessSuccessfulNTSMessageQueue)) t1.Start() t2.Start() t3.Start() t1.Join() t2.Join() t3.Join() </code></pre> <p>I have two threads (1 &amp; 2) reading a Queue of XML messages and posting the messages via HTTP to a web server. Once a message is posted it is dequeued from this queue added to a second queue. Thread 3 reads this queue and simply writes the XML out to a file.</p> <p>I would expect to see XML files gradually appearing as threads 1 &amp; 2 process the message queue (sometimes the message queue can take about 40 minutes to process). What I am actually seeing is that no XML files appear. I am I misunderstanding the code?</p> <p>*Edit. Here's the code for the four relevent threaded methods:</p> <pre><code>Private Sub ProcessNTSMessageQueue() While True Dim msg As String = Nothing SyncLock _MessageQueue If _MessageQueue.Count &gt; 0 Then msg = _MessageQueue.Dequeue() Else Exit Sub End If End SyncLock 'Post the message 'it's important to do this outside lock() If msg &lt;&gt; Nothing Then Me.PostXMLToNTS(msg) End If End While End Sub Private Sub ProcessSuccessfulNTSMessageQueue() While True Dim msg As String = Nothing SyncLock _SentMessageQueue If _SentMessageQueue.Count &gt; 0 Then msg = _SentMessageQueue.Dequeue() Else Exit Sub End If End SyncLock 'Post the message 'it's important to do this outside lock() If msg &lt;&gt; Nothing Then Me.ArchiveXMLAsFile(Guid.NewGuid.ToString, msg) _SuccessfulMessageCount += 1 End If End While End Sub Private Function PostXMLToNTS(ByVal XMLString As String) As Boolean Try Dim result As Net.HttpStatusCode = NTSPoster.PostXml(XMLString, _Settings.NTSPostURLCurrent, _Settings.NTSPostUsernameCurrent, _Settings.NTSPostPasswordCurrent) Select Case result Case Net.HttpStatusCode.Accepted, Net.HttpStatusCode.OK 'Good If _SentMessageQueue Is Nothing Then _SentMessageQueue = New Queue(Of String) End If _SentMessageQueue.Enqueue(XMLString) Return True Case Else 'Probably bad If _FailedMessageQueue Is Nothing Then _FailedMessageQueue = New Queue(Of String) End If _FailedMessageQueue.Enqueue(XMLString) Return False End Select Catch ex As Exception Throw End Try End Function Private Sub ArchiveXMLAsFile(ByVal name As String, ByVal XML As String) Try 'Create directory in archive location based on todays date Dim dir As New DirectoryInfo(Me.TodaysXMLArchiveLocation) 'If the directory does not already exist then create it If Not dir.Exists Then dir.Create() End If Dim FileName As String = String.Format("{0}.xml", name) Using sw As StreamWriter = New StreamWriter(Path.Combine(dir.FullName, FileName)) sw.Write(XML) sw.Close() End Using Catch ex As Exception Throw End Try End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload