Ejemplo n.º 1
0
        // Each thread runs the dispatch loop.

        private void DispatchLoop()
        {
            while (true)
            {
                var wi = default(WorkItem);

                lock (m_queue)
                {
                    // If shutdown was requested, exit the thread.
                    if (m_shutdown)
                    {
                        return;
                    }

                    // Find a new work item to execute.
                    while (m_queue.TryDequeue(out wi) == false)
                    {
                        m_threadsWaiting++;
                        try { Monitor.Wait(m_queue); }
                        finally { m_threadsWaiting--; }

                        // If we were signaled due to shutdown, exit the thread.
                        if (m_shutdown)
                        {
                            return;
                        }
                    }

                    // We found a work item! Grab it ...
                }

                // ...and Invoke it. Note: exceptions will go unhandled (and crash).
                wi.Invoke();
            }
        }
Ejemplo n.º 2
0
        public void LimitedSizeQueueLimitsTheSizeOfTheInternalQueue()
        {
            var q = new LimitedSizeQueue <int>(3);

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(3, q.Limit);
            Assert.IsFalse(q.TryPeek(out _));
            Assert.IsFalse(q.TryDequeue(out _));

            Assert.IsTrue(q.TryEnqueue(1));
            Assert.IsTrue(q.TryEnqueue(2));
            Assert.IsTrue(q.TryEnqueue(3));
            Assert.IsFalse(q.TryEnqueue(4));
            Assert.IsFalse(q.TryEnqueue(5));

            var items = string.Join(", ", q.Select(i => i.ToString()).ToArray());

            Assert.AreEqual("1, 2, 3", items);

            Assert.IsTrue(q.TryDequeue(out var item));
            Assert.AreEqual(1, item);

            Assert.IsTrue(q.TryEnqueue(6));
            Assert.IsFalse(q.TryEnqueue(7));

            Assert.IsTrue(q.TryDequeue(out item));
            Assert.AreEqual(2, item);

            Assert.IsTrue(q.TryDequeue(out item));
            Assert.AreEqual(3, item);

            Assert.IsTrue(q.TryPeek(out item));
            Assert.AreEqual(6, item);

            Assert.IsTrue(q.TryDequeue(out item));
            Assert.AreEqual(6, item);

            Assert.IsFalse(q.TryPeek(out _));
            Assert.IsFalse(q.TryDequeue(out _));
        }