MessageQueue queue = new MessageQueue(".\\myQueue"); queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); IAsyncResult asyncResult = queue.BeginReceive(); // Wait for the message to arrive asyncResult.AsyncWaitHandle.WaitOne(); // Get the received message Message message = queue.EndReceive(asyncResult); string body = (string)message.Body; Console.WriteLine("Received message: {0}", body);
MessageQueue queue = new MessageQueue(".\\myQueue"); queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); while (true) { IAsyncResult asyncResult = queue.BeginReceive(); // Wait for the message to arrive asyncResult.AsyncWaitHandle.WaitOne(); // Get the received message Message message = queue.EndReceive(asyncResult); string body = (string)message.Body; Console.WriteLine("Received message: {0}", body); }This code is similar to the previous example, but instead of receiving only one message, it continuously receives messages from the queue in a loop. This is achieved by wrapping the message receiving code in an infinite while loop. Package library: System.Messaging is a built-in package library in .NET Framework.