Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Read Message From MSMQ

1. Read Message From Queue

In the previous article, we looked at writing into the message queue. We wrote separate messages and a class instance to it. In this brief article, we will see how we can get Message from the Microsoft Message Queue (MSMQ).

2. About This Read Message MSMQ Example

In this example, we read message queue through the C# windows Form application. Its screenshot is below:

Reading From MSMQ

Reading From MSMQ

We will create this sample using C# Windows Application Project. By clicking the Read Messages, we read messages from the private queue (MSMQ) Q1 and display that in the text box shown. One can also use formatters while reading and writing the messages. The formatters are a separate topic to dig into. Here, in this example, we will concentrate on reading messages.

3. Code Explanation

3.1 Assembly Namespace to MSMQ

As told in the previous article on dot net remoting, first we add a reference to the MSMQ assembly (System. Messaging). Once we hold the reference in hand, we can use it in our C# code files. So first we provided the using statement in the form’s ‘.cs’ file.

3.2 Queue Name With Path

Here, we declare a class level variable


. It is holding the Queue with a path to it. This Messaging API will refer to this queue when the user clicks the Read Messages button. Note the ‘.’ notation specifies the root of the queue structure in MSMQ message tree.

3.3 Queue Reference

Next, we supply the queue name to the


constructor. Note we formed this queue name along with the path in the previous section. The variable Q1 is holding reference for this Message Queue which we wrote in the Previous article: Writing to Message Queue Q1. We will use this reference Q1 to call the MSMQ API functions.

3.4 Reading Messages From Queue

When we execute the Sample discussed in the previous article on dot net remoting, we post four messages in the message queue named Q1.


function call of the


will return the collection all these messages that is not yet read from the queue. In the below code we store all the messages in a 



3.5 Read Message Content

Next, we iterate through the


array and read a single message in each iteration from that array. Once we have the message, we are interested in the message content. The


property of the


Instance gives the message content. This body stream is an ‘


’ instance. As message body is in the form of a stream, we used the


 method to retrieve the streaming content in the byte array of 1024 max.

3.6 Display the Message String

To display the message in the text box, we convert the byte array into a text string using the


. Note, we use


method from


class from


 pack. Once we have the body of the message in the form of a string, we display that in the text box. The code is below:

4. Running Our Example

The below video shows the sample in action. Note that the video shows writing into the message queue using the sample created in the early article on dot net remoting and then reading those messages from the example made in this article.

Source Code: Download MSMQ Read Example From Google Drive

Categories: Remoting

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.