Replace MQC.MQGMO_BROWSE_FIRST with MQC.MQGMO_BROWSE_NEXT and call Get in a loop until it fails - it will throw a harmless exception with a reason code of MQRC_NO_MSG_AVAILABLE when there are no more messages to browse. I would also suggest that you use MQGMO_NO_WAIT rather than MQGMO_WAIT so that you don't have to wait until a message appears on the queue when you browse an empty queue. Example code using home-brewed helper methods:
public bool browseMessages(out ArrayList messages, string queue)
{
// browse (i.e. get without removing) a message on the message queue
messages = new ArrayList();
MQQueueManager mqQM = null;
MQQueue mqQ = null;
try
{
if (queue == null || queue.Length == 0)
throw new Exception("Queue name required");
// connect to queue manager
connectManager(out mqQM);
// open queue with required access
connectQueue(mqQM, queue, MQC.MQOO\_FAIL\_IF\_QUIESCING + MQC.MQOO\_BROWSE, out mqQ);
// get message options
MQGetMessageOptions mqGMO = new MQGetMessageOptions();
mqGMO.Options = MQC.MQGMO\_FAIL\_IF\_QUIESCING + MQC.MQGMO\_NO\_WAIT + MQC.MQGMO\_BROWSE\_NEXT; // browse with no wait
mqGMO.MatchOptions = MQC.MQMO\_NONE; // no matching required
MQMessage mqMsg = new MQMessage(); // object to receive the message
// browse all messages on the queue
while (true)
{
// browse message - exception will be thrown when no more messages
mqQ.Get(mqMsg, mqGMO);
// get message text from the message object
messages.Add(mqMsg.ReadString(mqMsg.MessageLength));
}
}
catch (MQException mqex)
{
if (mqex.ReasonCode == MQC.MQRC\_NO\_MSG\_AVAILABLE)
{
// harmless exception MQ throws to indicate that there are no messages on the queue
return true;
}
else
throwMQException(mqex);
}
finally
{
if (mqQ != null)
mqQ.Close();
if (mqQM != null)
mqQM.Disconnect();
}
return false;
}
Gavin