public QueueFetchResult GetUnprocessedMessages(QueueFetchUnprocessedMessagesRequest req)
        {
            var result = new QueueFetchResult();
            var queues = _monitorMsmqQueues.Where(q => q.Queue.Type == req.Type);

            if (queues.Count() == 0)
            {
                result.Items = new List<QueueItem>();
                return result;
            }

            List<QueueItem> r = new List<QueueItem>();
            result.Items = r;

            foreach (var q in queues)
            {
                var msmqQueue = q.Main;

                if (IsIgnoredQueue(q.Queue.Name) || !q.Main.CanRead)
                    continue;

                SetupMessageReadPropertyFilters(q.Main, q.Queue.Type);

                // Add peaked items
                lock (_peekItemsLock)
                {
                    if (_peekedItems.Count > 0)
                    {

                        r.AddRange(_peekedItems);
                        _peekedItems.Clear();
                    }
                }

                try
                {
                    var msgs = q.Main.GetAllMessages();
                    result.Count += (uint)msgs.Length;

                    foreach (var msg in msgs)
                    {

                        QueueItem itm = req.CurrentItems.FirstOrDefault(i => i.Id == msg.Id);

                        if (itm == null && !r.Any(i => i.Id == msg.Id))
                        {
                            itm = CreateQueueItem(q.Queue, msg);

                            // Load Message names and check if its not an infra-message
                            if (!PrepareQueueItemForAdd(itm))
                                itm = null;
                        }

                        if (itm != null)
                            r.Insert(0, itm);

                        // Just fetch first 500
                        if (r.Count > SbmqSystem.MAX_ITEMS_PER_QUEUE)
                            break;
                    }

                }
                catch (Exception e)
                {
                    OnError("Error occured when processing queue " + q.Queue.Name + ", " + e.Message, e, false);
                }

            }

            return result;
        }
        public QueueFetchResult GetProcessedMessages(QueueType type, DateTime since, IEnumerable<QueueItem> currentItems)
        {
            var result = new QueueFetchResult();

            var queues = GetQueueListByType(type);

            if (queues.Count() == 0)
            {
                result.Items = new List<QueueItem>();
                return result;
            }

            List<QueueItem> r = new List<QueueItem>();
            result.Items = r;

            foreach (var q in queues)
            {
                string qName = q.GetDisplayName();

                if (IsIgnoredQueue(qName) || !q.CanReadJournalQueue)
                    continue;

                SetupMessageReadPropertyFilters(q.Journal, type);

                try
                {
                    List<Message> messages = new List<Message>();

                    // Enumete from the earliest item
                    MessageEnumerator msgs = q.Journal.GetMessageEnumerator2();
                    try
                    {
                        while (msgs.MoveNext())
                        {
                            Message msg = msgs.Current;

                            if (msg.ArrivedTime >= since)
                                messages.Add(msg);
                        }
                    }
                    finally
                    {
                        msgs.Close();
                    }

                    foreach (var msg in messages)
                    {
                        QueueItem itm = currentItems.FirstOrDefault(i => i.Id == msg.Id);

                        if (itm == null)
                        {
                            itm = CreateQueueItem(q.Queue, msg);
                            itm.Processed = true;

                            if (!PrepareQueueItemForAdd(itm))
                                itm = null;
                        }

                        if (itm != null)
                            r.Insert(0, itm);
                    }
                }
                catch (Exception e)
                {
                    OnError("Error occured when getting processed messages from queue \"" + qName + "\", " + e.Message, e, false);
                }
            }

            result.Count = (uint)r.Count;

            return result;
        }
        public QueueFetchResult GetProcessedMessages(Model.QueueType type, DateTime since, IEnumerable<Model.QueueItem> currentItems)
        {
            var r = new QueueFetchResult();

              r.Count = 0;
              return r;
        }
        public static Model.QueueFetchResult GetUnprocessedMessages(QueueFetchUnprocessedMessagesRequest req, IEnumerable<AzureMessageQueue> monitoringQueues, Func<QueueItem, bool> prepareAddFunc)
        {
            var result = new QueueFetchResult(QueueFetchResultType.Cumulative);
              result.Status = QueueFetchResultStatus.NotChanged;

              IEnumerable<AzureMessageQueue> queues = monitoringQueues.Where(q => q.Queue.Type == req.Type);

              if( queues.Count() == 0 ) {
            result.Items = EMPTY_LIST;
            return result;
              }

              List<QueueItem> r = new List<QueueItem>();
              result.Items = r;

              foreach( var q in queues ) {
            //var azureQueue = q.Main;

            //if( IsIgnoredQueue(q.Queue.Name) )
            //  continue;

            var queueItemsCount = GetAzureQueueCount(q.Queue.Name);

            try {

              if( q.HasChanged(queueItemsCount) ) {  //q.HasChanged(req.TotalCount) ) {

            if( result.Status == QueueFetchResultStatus.NotChanged )
              result.Status = QueueFetchResultStatus.OK;

            long msgCount = q.GetMessageCount();
            SetAzureQueueCount(q.Queue.Name, (uint)msgCount);

            if( msgCount > 0 ) {
              var msgs = q.Main.PeekBatch(0, SbmqSystem.MAX_ITEMS_PER_QUEUE); // Need to specify 0 as it otherwise just retrieves new ones since last call

              if( msgs.Any() ) {
                long seqNr = 0;
                do {
                  _log.Trace("About to retrieve " + msgs.Count());

                  foreach( var msg in msgs ) {
                    if( seqNr == msg.SequenceNumber)
                      continue;

                    QueueItem itm = req.CurrentItems.FirstOrDefault(i => i.Id == msg.MessageId);

                    if( itm == null && !r.Any(i => i.Id == msg.MessageId) ) {
                      itm = CreateQueueItem(q.Queue, msg);

                      // Load Message names and check if its not an infra-message
                      if( !prepareAddFunc(itm) )
                        itm = null;

                      // Move this up to the Core
                      /*
                      if( q.Queue.Type == QueueType.Error && itm.Error != null &&
                          itm.Error.OriginQueue.IsValid() ) {

                        var i = itm.Error.OriginQueue.IndexOf('@');
                        var errorQueue = ( i > -1 ) ? itm.Error.OriginQueue.Substring(0, i) : itm.Error.OriginQueue;

                        if( !monitoringQueues.Any( mq => string.Compare(mq.Queue.Name, errorQueue, true) == 0 ) )
                          itm = null;
                      }
                      */
                    }

                    if( itm != null )
                      r.Insert(0, itm);

                    seqNr = msg.SequenceNumber;
                  }

                  if( r.Count >= msgCount || seqNr == 0 )
                    break;

                  msgs = q.Main.PeekBatch(seqNr, SbmqSystem.MAX_ITEMS_PER_QUEUE);

                } while( msgs.Count() > 1 );

                //q.LastSequenceNumber = seqNr;
              }

              result.Count = (uint)msgCount; // (uint)r.Count;
              //SetRealQueueCount(q.Queue.Name, result.Count);

            }
              }

            } catch( MessagingCommunicationException mce ) {
              //OnWarning(mce.Message, null, Manager.WarningType.ConnectonFailed);
              result.Status = QueueFetchResultStatus.ConnectionFailed;
              result.StatusMessage = mce.Message;
              break;

            } catch( SocketException se ) {
              //OnWarning(se.Message, null, Manager.WarningType.ConnectonFailed);
              result.Status = QueueFetchResultStatus.ConnectionFailed;
              result.StatusMessage = se.Message;
              break;

            } catch( Exception e ) {
              //OnError("Error occured when processing queue " + q.Queue.Name + ", " + e.Message, e, false);
              result.Status = QueueFetchResultStatus.HasErrors;
              result.StatusMessage = e.Message;
            }

              }

              return result;
        }
        public Model.QueueFetchResult GetUnprocessedMessages(QueueType type, IEnumerable<QueueItem> currentItems)
        {
            var result = new QueueFetchResult();
              result.Status = QueueFetchResultStatus.NotChanged;

              IEnumerable<AzureMessageQueue> queues = type != QueueType.Error ? _monitorQueues.Where(q => q.Queue.Type == type) : _monitorQueues.Where( q => q.IsDeadLetterQueue || q.Queue.Type == QueueType.Error );

              if( queues.Count() == 0 ) {
            result.Items = EMPTY_LIST;
            return result;
              }

              List<QueueItem> r = new List<QueueItem>();
              result.Items = r;

              foreach( var q in queues ) {
            var azureQueue = q.Main;

            //if( IsIgnoredQueue(q.Queue.Name) )
            //  continue;

            try {

              if( q.HasChanged() ) {

            if( result.Status == QueueFetchResultStatus.NotChanged )
              result.Status = QueueFetchResultStatus.OK;

            long msgCount = q.GetMessageCount();

            if( msgCount > 0 ) {
              var msgs = q.Main.PeekBatch(0, MessageCountLimit);
              result.Count += (uint)msgCount;

              foreach( var msg in msgs ) {
                QueueItem itm = currentItems.FirstOrDefault(i => i.Id == msg.MessageId);

                if( itm == null && !r.Any(i => i.Id == msg.MessageId) ) {
                  itm = CreateQueueItem(q.Queue, msg);

                  // Load Message names and check if its not an infra-message
                  if( !PrepareQueueItemForAdd(itm) )
                    itm = null;
                }

                if( itm != null )
                  r.Insert(0, itm);

              }
            }

              }

            } catch( MessagingCommunicationException mce ) {
              OnWarning(mce.Message, null, Manager.WarningType.ConnectonFailed);
              result.Status = QueueFetchResultStatus.ConnectionFailed;
              break;

            } catch( SocketException se ) {
              OnWarning(se.Message, null, Manager.WarningType.ConnectonFailed);
              result.Status = QueueFetchResultStatus.ConnectionFailed;
              break;

            } catch( Exception e ) {
              OnError("Error occured when processing queue " + q.Queue.Name + ", " + e.Message, e, false);
              result.Status = QueueFetchResultStatus.HasErrors;
            }

              }

              return result;
        }
    public static Model.QueueFetchResult GetUnprocessedMessages(QueueFetchUnprocessedMessagesRequest req, IEnumerable<AzureMessageQueue> queues, Func<QueueItem, bool> prepareAddFunc) {
      var result = new QueueFetchResult();
      result.Status = QueueFetchResultStatus.NotChanged;

      if( queues.Count() == 0 ) {
        result.Items = EMPTY_LIST;
        return result;
      }

      List<QueueItem> r = new List<QueueItem>();
      result.Items = r;

      foreach( var q in queues ) {
        //var azureQueue = q.Main;

        //if( IsIgnoredQueue(q.Queue.Name) )
        //  continue;
        
        var queueItemsCount = GetAzureQueueCount(q.Queue.Name);

        try {

          if( q.HasChanged(queueItemsCount) ) {  //q.HasChanged(req.TotalCount) ) {

            if( result.Status == QueueFetchResultStatus.NotChanged )
              result.Status = QueueFetchResultStatus.OK;

            long msgCount = q.GetMessageCount();
            SetAzureQueueCount(q.Queue.Name, (uint)msgCount);

            if( msgCount > 0 ) {
              var msgs = q.Main.PeekBatch(SbmqSystem.MAX_ITEMS_PER_QUEUE);
              result.Count += (uint)msgs.Count(); // msgCount

              foreach( var msg in msgs ) {

                QueueItem itm = req.CurrentItems.FirstOrDefault(i => i.Id == msg.MessageId);

                if( itm == null && !r.Any(i => i.Id == msg.MessageId) ) {
                  itm = CreateQueueItem(q.Queue, msg);

                  // Load Message names and check if its not an infra-message
                  if( !prepareAddFunc(itm) )
                    itm = null;
                }

                if( itm != null )
                  r.Insert(0, itm);

              }
            }
          }

        } catch( MessagingCommunicationException mce ) {
          //OnWarning(mce.Message, null, Manager.WarningType.ConnectonFailed);
          result.Status = QueueFetchResultStatus.ConnectionFailed;
          result.StatusMessage = mce.Message;
          break;

        } catch( SocketException se ) {
          //OnWarning(se.Message, null, Manager.WarningType.ConnectonFailed);
          result.Status = QueueFetchResultStatus.ConnectionFailed;
          result.StatusMessage = se.Message;
          break;

        } catch( Exception e ) {
          //OnError("Error occured when processing queue " + q.Queue.Name + ", " + e.Message, e, false);
          result.Status = QueueFetchResultStatus.HasErrors;
          result.StatusMessage = e.Message;
        }

      }

      return result;
    }