Click here to Skip to main content
Click here to Skip to main content

Using MSMQ from C#

, 11 Jan 2004
Rate this:
Please Sign up or sign in to vote.
Using MSMQ from C#

Introduction

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 inter-process 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.

Details

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

if(MessageQueue.Exists(@".\Private$\MyQueue"))
      //creates an instance 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 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();
mm.Body = txtMsg.Text;
mm.Label = "Msg" + j.ToString();
j++;
mq.Send(mm);

Use 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 locking access, properties and format 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 don’t 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, you’ll get the simple inter-process communication between the two forms.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Mercy Ponnupandy
Architect
United States United States
Architect / Consultant on Content Management and Cloud Computing... Artist and Author by the weekends...
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberdallas1234522-Aug-09 15:18 
Questioncan't install MSMQ Pinmemberprolingua.geo29-May-09 0:04 
AnswerRe: can't install MSMQ PinmemberLesF3-Dec-09 11:38 
GeneralGreat introduction PinmemberDUMITRU Guraliuc8-Apr-09 21:31 
GeneralTerrible example PinmemberAlanSimes20-Nov-06 22:24 
GeneralRe: Terrible example PinmemberLesF3-Dec-09 11:33 
GeneralVery Urgent PinmemberShiv55-Jun-06 20:54 
GeneralMultiple Clients over the internet Pinmembergianmaria24-Feb-06 3:33 
GeneralObjects Pinmembergianmaria24-Feb-06 3:12 
GeneralRe: Objects PinmemberBested8-May-06 0:10 
GeneralRe: Objects PinmemberSupermanDT2-Jun-06 12:44 
GeneralMessaging not installed PinmemberOldnavigator19-Jan-06 23:16 
GeneralRe: Messaging not installed Pinmemberawj10023-Feb-06 9:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411019.1 | Last Updated 12 Jan 2004
Article Copyright 2004 by Mercy Ponnupandy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid