Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an item to the front of the queue?

Tags:

I'm creating a Windows service that makes use of a FileSystemWatcher to monitor a particular folder for additions of a particular file type. Due the gap between the Created event and when the file is actually ready to be manipulated, I created a Queue<T> to hold the file names that need processing. In the Created event handler, the item is added to the queue. Then using a timer, I periodically grab the first item from the queue and process it. If the processing fails, the item is added back to the queue so the service can retry processing it later.

This works fine but I've found it has one side-effect: the first processing attempt for new items does not happen until all the old retry items have been retried. Since it's possible the queue could contain many items, I'd like to force the new items to the front of the queue so they are processed first. But from the Queue<T> documentation, there is no obvious way of adding an item to the front of the queue.

I suppose I could create a second queue for new items and process that one preferentially but having a single queue seems simpler.

So is there an easy way to add an item to the front of the queue?

like image 336
Corin Avatar asked Jan 13 '11 00:01

Corin


People also ask

How do I add something to the end of a queue?

To add an element at the end is therefore to set the next variable of the current last element to the element added and setting its next variable to null (to indicate that it's the new last element).


2 Answers

It kind of looks like you want a LinkedList<T>, which allows you to do things like AddFirst(), AddLast(), RemoveFirst(), and RemoveLast().

like image 174
CanSpice Avatar answered Oct 01 '22 13:10

CanSpice


Simply use the Peek method in your timer callback instead of Dequeue. If processing succeeds, then dequeue the item.

like image 20
500 - Internal Server Error Avatar answered Oct 01 '22 12:10

500 - Internal Server Error