C# - Simple example implementing concurrent queue

1 minute read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
        }
    }
}