title: Read all messages in MQ at once
body: <p>I am trying to read messages from MQ all at once by iterating through it. Currently I am only able to read one message at a time. I want to have a counter which will control how many times the loop will get executed or something like a while loop which will terminate when the queue is empty or else will continue reading message.</p>
  
  <pre><code>public string ReadMessages()
          {
              MQQueue mqDestination;
              String Readmessage = null;
              QueueManagerName = ConfigurationManager.AppSettings["QueueManagername"];
              Hashtable properties = new Hashtable();
              properties.Add(MQC.HOST_NAME_PROPERTY, ConfigurationManager.AppSettings["Connection"]);
              properties.Add(MQC.PORT_PROPERTY, ConfigurationManager.AppSettings["PortNo"]);
              properties.Add(MQC.CHANNEL_PROPERTY, ConfigurationManager.AppSettings["Channelname"]);            
              properties.Add(MQC.MQCA_TOPIC_NAME, ConfigurationManager.AppSettings["Queuename"]);
  
              properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
              queueManager = new MQQueueManager(QueueManagerName, properties);
  
              int openOptionsForGet = (MQC.MQSO_CREATE
                                          + (MQC.MQSO_FAIL_IF_QUIESCING
                                          + (MQC.MQSO_MANAGED
                                          + (MQC.MQSO_NON_DURABLE + MQC.MQMO_NONE))));
  
  
              MQGetMessageOptions Gmo;
              MQMessage RetrievedMessage;
              RetrievedMessage = new MQMessage();
              try
              {
                  RetrievedMessage = new MQMessage();
                  Gmo = new MQGetMessageOptions();
                  //Queue Name
                  mqDestination = queueManager.AccessQueue(ConfigurationManager.AppSettings["QueueName"], openOptionsForGet);
                  mqDestination.Get(RetrievedMessage, Gmo);
                  string message = RetrievedMessage.ReadString(RetrievedMessage.MessageLength);
                  queueManager.Disconnect();
              }
              catch (MQException ex)
              {
                  switch (ex.ReasonCode)
                  {
                      case IBM.WMQ.MQC.MQRC_NO_MSG_AVAILABLE:
                          Library.ErrorLogs("error" + "No message available.");
                          break;
                      case IBM.WMQ.MQC.MQRC_Q_MGR_QUIESCING:
                      case IBM.WMQ.MQC.MQRC_Q_MGR_STOPPING:
                          Library.ErrorLogs("error" + "Queue Manager Stopping: " + ConfigurationManager.AppSettings["QueueManagername"] + "\t" + ex.Message);
                          break;
                      case IBM.WMQ.MQC.MQRC_Q_MGR_NOT_ACTIVE:
                      case IBM.WMQ.MQC.MQRC_Q_MGR_NOT_AVAILABLE:
                          Library.ErrorLogs("error" + "Queue Manager not available: " + ConfigurationManager.AppSettings["QueueManagername"] + "\t" + ex.Message);
                          break;
                      default:
                          Library.ErrorLogs("error" + " Error reading topic: " + ConfigurationManager.AppSettings["Queuename"] + "\t" + ex.Message);
                          break;
                  }
  
              }
              catch (Exception ex)
              {
                  // Console.WriteLine("MQException caught. " + mqE.ToString());
                  Library.ErrorLogs("error" + ex.Message);
                  queueManager.Disconnect();
  
              }
             return Readmessage;
  
  
          }
  </code></pre>