Lucas,
Here is a prior post I made regarding this subject.
Normally I have a single worker thread, Looking at my code below, I believe
it will work with multiple worker threads.
For Queue I normally use a System.Threading.AutoResetEvent along with the
Queue. Depending on the parameters you pass to the AutoResetEvent.WaitOne
method, the background thread will sleep indefinitely waiting for the event
to be signaled.
The background thread has two loops. The outer loop does an
AutoResetEvent.WaitOne waiting for the event to be signaled. When the event
is signaled the background thread has an inner loop processing each item in
the queue.
When other threads put work items into the queue they Set the above
AutoResetEvent, letting the background thread know there is at least one
item in the queue.
Normally I put the Thread, the AutoResetEvent, the Queue and the padlock for
the Queue into a single class that represents the "Worker". Encapsulating
the above into clean type safe methods.
Something like:
' untested, typed from memory.
Public Class ThreadRequestQueue
Private Readonly m_padlock As New Object
Private Readonly m_queue As New Queue
Private Readonly m_event As New AutoResetEvent(False)
Public Sub AddRequest(ByVal request As Object)
SyncLock m_padlock
m_queue.Enqueue(Object)
End SyncLock
m_event.Set()
End Sub
Public Function GetRequest() As Object
' Check to see if there are already items available
SyncLock m_padlock
If m_queue.Count() > 0 Then
Return m_queue.DeQueue()
End If
End SyncLock
' Cannot block main thread
' while waiting for the main thread to add requests
' hence no SyncLock here
m_event.WaitOne()
' There must be an item
SyncLock m_padlock
Return m_queue.Dequeue()
End SyncLock
End Function
End Class
Hope this helps
Jay
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
news:ub**************@TK2MSFTNGP10.phx.gbl:
The main thread would add work items to a System.Collections.Queue,
the 2 worker threads would read work items from the queue in a loop.
When a worker thread is done with one work item, it would loop back &
read another item.
Hmmm that's a good idea too. Never thought of that : )
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/