.NET Queue - Dequeue and Peek

My question is about managing a Queue collection in VB.NET, and using Dequeue and Peek to handle the elements of the queue.

I'm using VB.NET, and I've implementing a Queue. The Queue is filled during a timer event. The timer checks the status of an external
operation, and adds a new item to the queue when the external operation is successful. This part seems to work fine.

I have a second timer that processes items in the Queue. This second process writes records to a database. For various reasons (which I cannot control), the second process can fail. I've wrapped the db stuff in a Transaction, so I can fail the entire process and not impact the database. This also works fine.

It's entirely possible during the db operations (in the second timer) that another item will be added to the Queue, so I'm hesitant to use Dequeue (since that removes the item from the Queue), and am instead considering using Peek.  My concern is that if I use Peek and then Dequeue after a successful db write, am I removing the correct element from the Queue?

For example:

<Timer1 - Add values to the Queue here>
Myque.Enque "value"

<Timer2 - Process values in the Queue here>
Dim val As String = Myque.Peek
<perform db operations here>
If DBSuccess Then
End If

So if I use PEEK, and then perform the DB operations, and THEN use DEQUEUE, can I be sure that I'm Dequeing the same element I handled when I used PEEK? My concern is during the db operation the first timer may have added another item to the Queue, which (to me) would mean that I'd be Dequeing that new item (which was not processed).

My first thought was to use Dequeue, and then try the db operations. If that failed, I'd use Enqueue to return that item to the queue. The order of elements in the queue is irrelevant, so there's no harm if an element is processed "out of order".

Or is there an entirely better way to handle this? I'm open to any suggestion.
LVL 86
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First, I would be using BackgroundWorker objects instead of timers.

How long does a DB process take?

You could SyncLock queue to avoid situation (lock it while doing DB op)

Or if you do Peek, you are looking at the first element.  So the next element to be DeQueued, even if you EnQueue 100 more in the mean time, would still be the first element (the one you peeked at). So you would be fine to do that also.

In a landing queue at the airport. If flight 102 is first in the queue, it doesn't matter if 10 more come into the queue, 102 is still going to get to land first.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Enqueue adds an object to the end of the Queue and Dequeue removes and returns the object at the beginning of the Queue. They work on different ends of the queue collection. So you can add many objects to a queue collection without effecting the next object to be removed from the queue collection.
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareAuthor Commented:
Thanks SStory and Fernando for your help. I didn't realize the Queue was a FIFO-sort of stack, so as you've both mentioned I'm fine with using PEEK.
I would be using BackgroundWorker objects instead of timers.
I didn't think of that. What would be the benefit of using the BackgroundWorkder versus a Timer? I've used BGW before, and I understand (to some degree) the concept of threads.

I'll open a new question regarding the BackGroundWorker, and will post back here with the link to that in case either of you are interested.

Thanks again - much appreciated!
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

AndyAinscowFreelance programmer / ConsultantCommented:
A general point.
If you are removing/appending items to a collection from different threads then you really ought to have an exclusion mechanism (eg. mutex) coded to lock the collection whilst one operation is executing.
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareAuthor Commented:
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareAuthor Commented:
Thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.