473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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!
Nov 20 '05 #1
2 1295
"Brian Henry" <brianiup[remove-me]@adelphia.net> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
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.
--
Regards,
Webster
Nov 20 '05 #2
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

"Brian Henry" <brianiup[remove-me]@adelphia.net> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
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!

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: newsock | last post by:
What differences between queue, deque and priority_queue? And under what situations choose one of them to use? Thanks for your help!
0
by: Jason | last post by:
I want to create a synchronised queue class which has very simple functionality as a queue but is thread safe, where two different threads can add and remove items in such a way that doesn't...
1
by: Macca | last post by:
Hi, I have an application that uses a queue. I have one thread that populates the queue and a second that takes items off of the queue and processes them At the moment all i have is these 2...
1
by: johnny | last post by:
hi all, I would like to understand which are the best ways to send bulk mailings like for newsletters and so on ( not spamming, always to receivers who opt-in ). I am not looking for code for...
7
by: RobKinney1 | last post by:
Here is another question we have been meaning to post for a long time. My colleague is literally pulling chunks of hair out of his head right now. We have a logging class. This class is...
2
by: lavender | last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10. Where is the wrong in my code? Why it cannot...
10
by: Craig Buchanan | last post by:
I would like to have a limited pool of objects (the objects are expensive to create) that can be enlisted to process items in a queue. Moreover, I would like to be able to have the processing...
12
by: Paul Rubin | last post by:
I'd like to suggest adding a new operation Queue.finish() This puts a special sentinel object on the queue. The sentinel travels through the queue like any other object, however, when...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.