C# – Simple example implementing concurrent queue

public class ActionQueueItem
{
    public ActionQueueItem(Action operation, string name, int priority)
    {
        Operation = operation;
        Delay = 500; // 500 milliseconds
        Name = name;
        Priority = priority;
    }

    public ActionQueueItem(Action operation, string name, int delay, int priority)
    {
        Operation = operation;
        Name = name;
        Delay = delay;
        Priority = priority;
    }

    public Action Operation { get; private set; }
    public int Delay { get; set; }
    public string Name { get; set; }
    public int Priority { get; set; }
}

public class ActionDelayQueue
{
    ManualResetEvent m_stopEvent = new ManualResetEvent(false);
    ManualResetEvent m_enqueuEvent = new ManualResetEvent(false);
    Thread m_processTask;

    List<ActionQueueItem> m_queue = new List<ActionQueueItem>();

    public ActionDelayQueue()
    {

    }

    public bool Start()
    {
        Stop();

        m_stopEvent.Reset();
        m_processTask = new Thread(Process);
        m_processTask.Start();
        return true;
    }

    public bool Stop()
    {
        if (m_processTask != null)
        {
            m_stopEvent.Set();
            if (!m_stopEvent.WaitOne(1000))
                m_processTask.Abort();

            m_processTask = null;
            return true;
        }

        return false;
    }

    public void Enqueue(ActionQueueItem item)
    {
        lock (m_queue)
        {
            if (item.Priority >= 5)
            {
                m_queue.Insert(0, item);
            }
            else
                m_queue.Add(item);
           
            m_enqueuEvent.Set();
        }
    }

    private void Process()
    {
        try
        {
            WaitHandle[] list = new WaitHandle[] { m_stopEvent, m_enqueuEvent };
            while (true)
            {
                var res = WaitHandle.WaitAny(list);
                if (0 == res)
                    break;
                else if (1 == res)
                {
                    ActionQueueItem action = null;
                    lock (m_queue)
                    {
                        if (m_queue.Count > 0)
                        {
                            var currentTime = DateTime.Now;
                            action = m_queue[0];
                            m_queue.RemoveAt(0);

                            if (m_queue.Count <= 0)
                                m_enqueuEvent.Reset();
                        }
                    }

                    if (action != null)
                    {
                        Thread.Sleep(action.Delay);
                        action.Operation();
                    }
                }
            }
        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message);
        }
    }
}
Advertisements
This entry was posted in .NET, Programming and tagged . Bookmark the permalink.

One Response to C# – Simple example implementing concurrent queue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s