MyQueue In C#

Introduction

Microsoft Windows Message Queuing is an 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 canceled. You can use Windows security to secure access control, authenticate, and encrypt the messages you send and receive.

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

queque1

queque2

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 that the other form has been 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 between creating a queue and creating an instance of the Message Queuing component, which refers to an already existing queue in the operating system. This code snippet says if the private queue called MyQueue exists, create an instance of MessageQueue to point to that queue. Otherwise, create a private queue called MyQueue.

if (MessageQueue.Exists(@".\Private$\MyQueue"))
{
    // Creates an instance of MessageQueue, which points to the already existing MyQueue
    mq = new System.Messaging.MessageQueue(@".\Private$\MyQueue");
}
else
{
    // Creates a new private queue called MyQueue
    mq = MessageQueue.Create(@".\Private$\MyQueue");
}

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

System.Messaging.Message mm = new System.Messaging.Message
{
    Body = txtMsg.Text,
    Label = "Msg" + j.ToString()
};
j++;
mq.Send(mm);

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

try
{
    mes = mq.Receive(new TimeSpan(0, 0, 3));
    mes.Formatter = new XmlMessageFormatter(new string[] { "System.String,mscorlib" });
    m = mes.Body.ToString();
}
catch
{
    m = "No Message";
}
MsgBox.Items.Add(m.ToString());

There are several considerations in retrieving and reading messages from the queue. They are blocking access, properties, and formats for reading messages. Here, I've dealt with format only. To read messages from a queue, a formatter is necessary to serialize and deserialize the message before manipulating it. I've 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 prevent other users from reading a message from the queue you are working on and also prevent them from removing messages.

Put the creation logic in the constructor of the form and the send and receive inappropriate button clicks, and you'll get the simple interprocess communication between the two forms.


Similar Articles