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?
<Timer1 - Add values to the Queue here>
<Timer2 - Process values in the Queue here>
Dim val As String = Myque.Peek
<perform db operations here>
If DBSuccess Then
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.