I have to implement a Delay Queue in C# as there is no standard implementation of delay queue present in the C#. I am looking to use System.Threading.Timer for implementing delayed enqueue of a node.
public class DelayQueue<T>
{
private Queue queue<T> = new Queue<T>();
public void Enqueue(Object object)
{
this.queue.Enqueue(object as T);
}
public void Enqueue(T node, TimeSpan dueTime)
{
new System.Threading.Timer(this.Enqueue, node, dueTime, -1);
}
.
.
.
}
This approach looks fine to me but since I am new to C#(from C background), I want someones opinion that whether it is the right way or are there any better and more effective methods of doing the same?
I don’t think that creating timers for each item is a good idea. Anyway, you only need to get first ready item when you dequeue item from queue, then you just can store time when item will be ready:
UPDATE (blocking queue with waiting timeout)
Usage: