/// <summary>
        /// Initializes a new instance of the <see cref="DbMessageQueue"/> class.
        /// </summary>
        /// <param name="dbConnection"></param>
        public DbMessageQueue(DbConnection dbConnection)
        {
            if (dbConnection == null)
            {
                throw new ArgumentNullException("dbConnection");
            }

            DbConnection      = dbConnection;
            _dbConnection     = dbConnection;
            _cancelledQueries = new HashSet <string>();
            EnumComparer <CommandPriority> comparer = EnumComparer <CommandPriority> .s_Instance;

            _queues = new Dictionary <CommandPriority, ThreadSafeMultiQueue <string, IDBCommand> >(comparer);

            foreach (CommandPriority priority in Enum.GetValues(typeof(CommandPriority)))
            {
                _queues[priority] = new ThreadSafeMultiQueue <string, IDBCommand>();
            }

            //multiQueue = new ThreadSafeQueue<string, IDBCommand>();

            _commandsToExecute = new Queue <IDBCommand>();

            _queueThread = new Thread(_QueueRunner)
            {
                Name = "CommandQueue", IsBackground = false
            };
            _queueThread.Start();
        }
        private void _ExecuteNonFilteredQueue(ThreadSafeMultiQueue <string, IDBCommand> multiQueue, CommandPriority currentPriority)
        {
            while (multiQueue.Count() > 0)
            {
                foreach (IDBCommand command in multiQueue)
                {
                    _ExecuteCommand(command);
                    if (_GetHighestNonEmptyPriority() < currentPriority)
                    {
                        break;
                    }
                }
            }

            GatLogger.Instance.AddMessage("DB Queue is empty.  You can exit at anytime.", LogMode.LogAndScreen);
        }
 /// <summary>
 /// Safely remove 1 by 1 commands from the queue and execute it
 /// </summary>
 public void Execute()
 {
     try
     {
         while (Count > 0) //as long as not all commands are in DB
         {
             CommandPriority priority = _GetHighestNonEmptyPriority();
             ThreadSafeMultiQueue <string, IDBCommand> multiQueue = _queues[priority];
             _ExecuteNonFilteredQueue(multiQueue, priority);
         }
     }
     catch (Exception e)
     {
         GatLogger.Instance.AddMessage(string.Format("DB Command failed with following message {0}\n{1}", e.Message, e.StackTrace));
         ErrorEventHandler(e);
         throw;
     }
 }
        /// <summary>
        /// Filters and executes the commands until multi queue getting full again and or command is new is queue
        /// </summary>
        private void _FilterAndExecuteCommands(ThreadSafeMultiQueue <string, IDBCommand> multiQueue, CommandPriority currentPriority)
        {
            bool queueTooLoaded = multiQueue.Count() > ct_queueThreshold;

            while (_commandsToExecute.Count > 0)
            {
                if (_GetHighestNonEmptyPriority() < currentPriority)
                {
                    return;
                }

                if (queueTooLoaded)
                {
                    //if filtering process began when queue was too loaded or there is blocking command in queue
                    //all commands that passed in the filter will be executed regardless of
                    //how much time they were in queue
                    _ExecuteCommand(_commandsToExecute.Dequeue());
                }
                else if (_MinimumCommandQueueTime.HasValue)
                {
                    //If commands was not in queue for enough time the execution process is halted
                    IDBCommand nextCommand     = _commandsToExecute.Peek();
                    DateTime   filterThreshold = nextCommand.GetQueueInsertionTime() + _MinimumCommandQueueTime.Value;
                    if (_lastFilterEndTime < filterThreshold)
                    {
                        return;
                    }

                    _ExecuteCommand(_commandsToExecute.Dequeue());
                }
                else
                {
                    throw new GatDataBaseException(
                              "filterAndExecuteCommands was called when queueTooLoaded = false and !parameters.MinimumCommandQueueTime.HasValue");
                }

                //If queue is getting loaded the execution process is halted
                if (multiQueue.Count() >= ct_queueThreshold)
                {
                    return;
                }
            }
        }