简体   繁体   中英

How to grab one MSMQ message at a time from the queue

I am using Console application (C#) to read the MSMQ messages.

The following is the working code.

C# (Console Application)

static void Main(string[] args)
{
     MessageQueue[] myQueueArray = MessageQueue.GetPrivateQueuesByMachine("192.0.0.1");

                    if (myQueueArray != null)
                    {                    
                        foreach (MessageQueue mq in myQueueArray)
                        {
                            if (mq.QueueName.Contains("myqueue"))
                            {
                                myQueue = mq;
                                break;
                            }
                        }

                        if (myQueue != null)
                        {

                            Message[] messageList = myQueue.GetAllMessages();
                            if (messageList != null)
                            {                           
                                foreach (Message msg in messageList)
                                {    
                                 //doing some operation with the message
                                }    
                            }    
                        }
                     }
                 }

The above code is working perfectly.

But I want to read the MSMQ messages one by one means how do i grab one message at a time from the queue?

You can use MessageQueue.Receive to dequeue single message at a time, see this MSDN article . You can also use BeginReceive and ReceiveCompleted event to get the notification, see this MSDN article .

Edit

This MSDN link that has very simple sample code that helped the OP to dequeue the message from MSMQ (pointed through comment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM