Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:30:43+00:00 2026-06-17T21:30:43+00:00

I have to implement a Delay Queue in C# as there is no standard

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:30:45+00:00Added an answer on June 17, 2026 at 9:30 pm

    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:

    public class DelayQueue<T>
    {
        private List<DelayQueueItem<T>> items = new List<DelayQueueItem<T>>();
    
        public void Enqueue(T item)
        {
            Enqueue(item, TimeSpan.Zero);
        }
    
        public void Enqueue(T item, TimeSpan delay)
        {
            items.Add(new DelayQueueItem<T>()
            {
                Value = item,
                ReadyTime = DateTime.Now.Add(delay)
            });
        }
    
        public T Dequeue()
        {
            DateTime now = DateTime.Now;
            var item = items.FirstOrDefault(i => i.ReadyTime <= now);
            if (item != null)
            {
                items.Remove(item);
                return item.Value;
            }
    
            return default(T);
        }
    
        private class DelayQueueItem<T>
        {
            public T Value { get; set; }
            public DateTime ReadyTime { get; set; }
        }
    }
    

    UPDATE (blocking queue with waiting timeout)

    public T Dequeue()
    {
        return Dequeue(TimeSpan.Zero);
    }
    
    public T Dequeue(TimeSpan timeout)
    {
        DateTime startTime = DateTime.Now;
    
        do
        {
            DateTime now = DateTime.Now;
    
            var item = items.FirstOrDefault(i => i.ReadyTime <= now);
            if (item == null)
                continue;
    
            items.Remove(item);
            return item.Value;
        }
        while (DateTime.Now - startTime < timeout);
    
        return default(T);
    }
    

    Usage:

    DelayQueue<string> queue = new DelayQueue<string>();
    queue.Enqueue("world", new TimeSpan(0, 0, 1));
    queue.Enqueue("hello");                        
    queue.Enqueue(",");
    
    TimeSpan timeout = new TimeSpan(0, 0, 2);
    Console.WriteLine(queue.Dequeue());
    Console.WriteLine(queue.Dequeue(timeout));
    Console.WriteLine(queue.Dequeue(timeout));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

clients :has_many :project,:roles I have implement but its not a good implementation..
After much delay, I realize I want to implement a version control system into
I have implement the exact same concept for my project- http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/ My question is,
I have implement a scenario which involves two way communication between child and parent
I have to implement a sparse matrix (a matrix that has predominantly zeroes, so
I have to implement an Image Gallery for the iPhone iOS. Pictures are loaded
I have to implement a iphone application which will record user's voice as you
I Have to implement on my app a little map of a concert hall.
I have to implement an application in which I have set small edittext for
I have to implement jquery's sortable in my project. I am creating it dynamically.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.