コード例 #1
0
        public void AddFiveItemsCopyToArray()
        {
            var linkedListQueueArray = new int[5];
            var arrayQueueArray      = new int[5];
            int linkedListQueueLast  = 0;
            int arrayQueueLast       = 0;

            for (int i = 1; i <= 5; i++)
            {
                linkedListQueue.Enqueue(i);
                arrayQueue.Enqueue(i);
            }
            foreach (var item in linkedListQueue.Get())
            {
                linkedListQueueLast = item;
            }

            foreach (var item in arrayQueue.Get())
            {
                arrayQueueLast = item;
            }

            linkedListQueue.CopyTo(linkedListQueueArray, 0);
            arrayQueue.CopyTo(arrayQueueArray, 0);

            Assert.AreEqual(linkedListQueueLast, 5, "invalid last element");
            Assert.AreEqual(arrayQueueLast, 5, "invalid last element");

            Assert.AreEqual(linkedListQueueArray[0], 1, "invalid element");
            Assert.AreEqual(arrayQueueArray[0], 1, "invalid element");

            Assert.AreEqual(linkedListQueueArray[4], 5, "invalid element");
            Assert.AreEqual(arrayQueueArray[4], 5, "invalid element");
        }
コード例 #2
0
ファイル: EventPump.cs プロジェクト: gixslayer/darktech
        /// <summary>
        /// Pump all the queued events to the designated handlers. This method should only ever be called from the game thread.
        /// </summary>
        internal void PumpEvents()
        {
            // Copy the front buffer to the back buffer before processing.
            // It's important to minimize time spend within the lock to avoid stalling other threads trying to enqueue events.
            lock (syncRoot)
            {
                frontBuffer.CopyTo(backBuffer);
                frontBuffer.Clear();
            }

            // The enumerator will preserve the correct queue order.
            foreach (Event e in backBuffer)
            {
                bool processed = false;

                // Enumerate through all the registered event dispatchers.
                foreach (EventDispatcher dispatcher in eventDispatchers)
                {
                    // If the dispatcher returns true the event has been processed and send to all registered handlers.
                    if (dispatcher.ProcessEvent(e))
                    {
                        processed = true;

                        break;
                    }
                }

                // If no event dispatcher processed the event throw out a warning.
                if (!processed)
                {
                    Engine.Log.WriteLine("warning/system", "Event {0} was not processed", e.Type);
                }
            }

            // Clear the back buffer to prevent holding references to otherwise potentially dead instances.
            backBuffer.Clear();
        }