best way to tell if a queue has items added to it while executeing

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

I need to have an app that will have a queue, but sit dormant until
something is placed in that queue, when it realizes the queue has items
waiting start executeing the processes for thoes items... whats the best way
to do this? I tried do loops and some attempts at threats for this... do
loops uses process speed up cause of the constant execuation which i dont
want... does anyone know of a way to instantaniously find out if a queue has
an item enqueued? and run a process.. thanks!
 
Brian Henry said:
I need to have an app that will have a queue, but sit dormant until
something is placed in that queue, when it realizes the queue has items
waiting start executeing the processes for thoes items... whats the best way
to do this? I tried do loops and some attempts at threats for this... do
loops uses process speed up cause of the constant execuation which i dont
want... does anyone know of a way to instantaniously find out if a queue has
an item enqueued? and run a process.. thanks!

What if you made your own queue class (or inherited etc) and then make an
event like ItemAdded? Then whenever you call the Enqueue() function, you
can raise the event ItemAdded.
 
Brian,
Are you using a System.Messaging.MessageQueue or a System.Collections.Queue
class?

For MessageQueue I normally use BeginReceive to wait for a Message to arrive
on the Queue.

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 worker thread
' while waiting for the worker threads to add requests
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
 
Back
Top