MyQueue in C#

Microsoft Windows Message Queuing is the easy way to communicate with application programs quickly and reliably by sending and receiving messages. Messaging provides a flexible and powerful mechanism for interprocess communication between the components of server-based applications.

It provides you with guaranteed message delivery. You can prioritize your messages according to your needs. Messages can be sent and remain in the queue the same way as it sent till the message is delivered. In other words, you can implement offline capabilities. Several related messages can be coupled into a single transaction to ensure that they are sent in order, delivered only once and successfully retrieved in the destination queue. If any error occurs in this transaction, the transaction is cancelled. You can use Windows security to secure access control, authenticate and encrypt the messages you send and receive.

This article attempts to explain the how MSMQ can be implemented between two forms and the very basics of message queuing. Here are the two forms.



When you enter a message in the textbox and click send, it sends the message to the other form. When you click Receive, you get the message what the other form has sent. When there is no message in the queue, it says No Message.

Let me start with the queue creation. Here, I create a private queue called MyQueue. You can create public queues on your machine or any machine with Message Queuing. For that, you need to have domain or enterprise administrative access rights. There is a difference in creating a queue and creating an instance of Message Queuing component, which refers to an already existing queue in the operating system. This code snippet says, if the private queue called MyQueue exist, create an instance of MessageQueue to point to that queue. Else, create a private queue called MyQueue.

//creates an instance MessageQueue, which points to the already existing MyQueue
mq = new System.Messaging.MessageQueue(@".\Private$\MyQueue");
//creates a new private queue called MyQueue
mq = MessageQueue.Create(@".\Private$\MyQueue");

You can verify whether the queue is created or not with Computer Management Console. Expand, Services and Applications in Computer Management Console. Expand, Message Queuing and click Private Queue. Youll find MyQueue here. Now, let us see how to send the messages.

System.Messaging.Message mm = new System.Messaging.Message();
mm.Body = txtMsg.Text;
mm.Label = "Msg" + j.ToString();

Use Message object to send messages. This will give you more control over your messages. To receive,

mes = mq.Receive(
new TimeSpan(0, 0, 3));
mes.Formatter =
new XmlMessageFormatter(new String[]
m = mes.Body.ToString();
m = "No Message";

There are several considerations in retrieving and reading messages from the queue. They are locking access, properties and format for reading messages. Here, Ive dealt with format only. To read messages from a queue, a formatter is necessary to serialize and deserialize the message before manipulating it. Ive used XmlMessageFormatter here.

You can set a series of properties to indicate what properties you want to retrieve when the component gets a message from a queue. You can find these properties in a class called MessagePropertyFilter Class and it corresponds to actual properties on the Message class. When you set the value for one of these properties to true, the component will retrieve the corresponding property each time a message is removed from the queue. If you dont want to retrieve any property, you can set the MessagePropertyFilter to false.

You can lock access to the queue by setting the DenySharedReceive property to false. This will help temporarily prevents other users to read message from the queue you are working and also prevents from removing messages.

Put the creation logic in the constructor of the form and the send and receive in appropriate button clicks, youll get the simple interprocess communication between the two forms.

Up Next
    Ebook Download
    View all
    View all