MSMQ development of interprocess communication under the. NET Compact Framework

Source: Internet
Author: User
Tags compact message queue msmq

The previous article, on the MSMQ installation under WinCE, describes the development of MSMQ under the. NET Compact Framework.

The so-called MQ is message queue. Message Queuing can serve as a conduit for communication between different applications, or even between different machines. The content of the communication under Message Queuing is called a message, and in the C # program the messages are the objects.

MSMQ is the MQ service program provided by Microsoft. The MQ service is responsible for managing Message Queuing, ensuring that messages are sent to the right end in the channel of Message Queuing, MQ supports offline transactions, and sometimes messages are cached in MQ service programs, and when the receiver is picking up messages at the time of the line. This feature allows MQ to be widely used in the mobile world because the network of mobile applications does not guarantee 7x24 long connections.

Build queues

To develop MQ under Cf.net, you need to refer to the System.Messaging library.

using System.Messaging;


public class Mqservice


    {


Private Const string mmachineprefix = @ ". \";


Private Const String Mprivatequeuenameprefix = Mmachineprefix + @ "private$\";


Private Const String Mservicequeuepath = Mprivatequeuenameprefix + "mqservicequeue$";


private MessageQueue Mservicequeue;





private void Initservicequeue ()


        {


//Create the message queue


Try


            {


//Check to make sure the message queue does not exist already


if (! Messagequeue.exists (Mservicequeuepath))


                {


//Create the new message queue and make it transactional


mservicequeue = messagequeue.create (Mservicequeuepath);


Mservicequeue.close ();


                }


Else


                {


mservicequeue = new MessageQueue (Mservicequeuepath);


                }





type[] types = new Type[1];


Types[0] = typeof (String);


mservicequeue.formatter = new XMLMessageFormatter (types);


mservicequeue.receivecompleted + = new Receivecompletedeventhandler (Messagelistenereventhandler);





//Begin the asynchronous receive operation.


mservicequeue.beginreceive ();


Mservicequeue.close ();


            }


//Show message if we used a invalid message queue name;


catch (messagequeueexception mqexception)


            {


Console.WriteLine (mqexception.message);


            }


return;


        }


    }





using System.Messaging;


public class Mqservice


    {


Private Const string mmachineprefix = @ "."


Private Const String Mprivatequeuenameprefix = Mmachineprefix + @ "private$";


Private Const String Mservicequeuepath = Mprivatequeuenameprefix + "mqservicequeue$";


private MessageQueue Mservicequeue;





private void Initservicequeue ()


        {


//Create the message queue


Try


            {


//Check to make sure the message queue does not exist already


if (! Messagequeue.exists (Mservicequeuepath))


                {


//Create the new message queue and make it transactional


mservicequeue = messagequeue.create (Mservicequeuepath);


Mservicequeue.close ();


                }


Else


{


mservicequeue = new MessageQueue (Mservicequeuepath);


                }





type[] types = new Type[1];


Types[0] = typeof (String);


mservicequeue.formatter = new XMLMessageFormatter (types);


mservicequeue.receivecompleted + = new Receivecompletedeventhandler (Messagelistenereventhandler);





//Begin the asynchronous receive operation.


mservicequeue.beginreceive ();


Mservicequeue.close ();


            }


//Show message if we used a invalid message queue name;


catch (messagequeueexception mqexception)


            {


Console.WriteLine (mqexception.message);


}


return;


        }


    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.