473,698 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1303
"Brian Henry" <brianiup[remove-me]@adelphia.net> wrote in message
news:Oc******** ******@TK2MSFTN GP12.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.Messagin g.MessageQueue or a System.Collecti ons.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.Threadin g.AutoResetEven t 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 ThreadRequestQu eue

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(ByVa l 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******** ******@TK2MSFTN GP12.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
12684
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
1490
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 interfere with each other. Will the following code suffice. Does the synchronised method return a queue that can only be accessed by a single thread at any one time. Also what happens if you try and remove (dequeue) an item when the queue is empty....
1
2280
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 threads. I may use another thread to process the items separately so that i now have one thread that puts items on the queue, another that takes items off the queue and a third that processes
1
3104
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 now , I have found some very interesting classes and I also have my own scripts, I just want to know all the options available and why one is better of another .
7
1654
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 mutlithreaded (as required by our boss). So when we tell the class to log something, it puts the data into a queue, and logs it to the disk when it has time later (low priority thread). Its simple. As stated above, I throw data into the queue. The...
2
2957
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 store up to 10 items? Output from my code: Enter you choice: 1 Enter ID document to print : 21 Enter you choice: 1 Enter ID document to print : 22
10
1403
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 (time-consuming) occur on a background thread. I have two questions: * what is a good way to block the queue loop while it waits for a free worker? * is my approach a reasonable one?
12
1404
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 q.get() encounters the sentinel, it raises StopIteration instead of returning the sentinel. It does not remove the sentinel from the queue, so further calls to q.get also raise StopIteration. That permits writing the typical "worker thread" as
4
4597
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 length of my queue. I started getting compilation errors when I included a length function. <code> template<class ItemType> void Queue<ItemType>::MakeEmpty() {
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.