Esempio n. 1
0
        /// <summary>
        /// Try to pop an object or create it if the stack is empty.
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            T obj = null;

            if (!m_objects.TryDequeue(out obj))
            {
                obj = m_creator();
            }
            return(obj);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="updateDelta"></param>
        public virtual void Update(long updateDelta)
        {
            UpdateTime += updateDelta;

            foreach (var timer in m_timerList.Where(timer => (UpdateTime - timer.LastActivated) >= timer.Delay))
            {
                try
                {
                    timer.Tick(UpdateTime);
                }
                catch (Exception ex)
                {
                    Logger.Error("TaskQueue[" + GetType().Name + "] timer update failed [" + timer.GetType().Name + "] : " + ex.ToString());
                }
                if (timer.OneShot)
                {
                    RemoveTimer(timer);
                }
            }

            foreach (var updatableObject in m_subUpdatableObjects)
            {
                try
                {
                    updatableObject.Update(updateDelta);
                }
                catch (Exception ex)
                {
                    Logger.Error("UpdatableObject[" + GetType().Name + "] update failed : " + ex.ToString());
                }
            }

            Action msg;

            while (m_messagesQueue.TryDequeue(out msg))
            {
                try
                {
                    msg();
                }
                catch (Exception ex)
                {
                    Logger.Error("UpdatableObject[" + GetType().Name + "] message failed to process : " + ex.ToString());
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        private void InternalUpdate()
        {
            var timeStart   = m_queueTimer.ElapsedMilliseconds;
            var updateDelta = timeStart - LastUpdate;

            LastUpdate = timeStart;

            foreach (var timer in m_timerList)
            {
                if ((LastUpdate - timer.LastActivated) >= timer.Delay)
                {
                    try
                    {
                        timer.Tick(LastUpdate);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("TaskQueue[" + Name + "] failed to update timer [" + timer.GetType().Name + "] : " + ex.ToString());
                    }
                    if (timer.OneShot)
                    {
                        RemoveTimer(timer);
                    }
                }
            }

            foreach (var updatableObject in m_updatableObjects)
            {
                try
                {
                    updatableObject.Update(updateDelta);
                }
                catch (Exception ex)
                {
                    Logger.Error("TaskQueue[" + Name + "] failed to update object [" + updatableObject.GetType().Name + "] : " + ex.ToString());
                }
            }

            Action msg = null;

            while (m_messageQueue.TryDequeue(out msg))
            {
                try
                {
                    msg();
                }
                catch (Exception ex)
                {
                    Logger.Error("TaskQueue[" + Name + "] failed to process message: " + ex.ToString());
                }
            }

            var timeStop     = m_queueTimer.ElapsedMilliseconds;
            var updateLagged = timeStop - timeStart > UpdateInterval;
            var nextDelay    = 0;

            if (!updateLagged)
            {
                nextDelay = (int)((timeStart + UpdateInterval) - timeStop);
            }

            if (m_running)
            {
                Task.Factory.StartNewDelayed(nextDelay, InternalUpdate);
            }
        }