First, install message Queue:
Before Win7, Control Panel, add the Delete component (Windows Message Queue).
WIN7~WIN8: Control Panel, programs and features, enabling or turning off Windows features (find Windows Message Queue Server) option, together with all subclasses, can be installed automatically.
Second, use the message Queue:
1) for communication between various types of servers and computers:
Locally, send yourself to yourself (directly the private name of the. \\Private$\\Queue).
Remote computer:
formatname:D irect=tcp:121.0.0.1\\private$\\queue Name
formatname:D irect=os:machinename\\private$\\queue name (only for computers that have been remotely added to the domain).
formatname:D irect=http://222.10.xx.xx/msmq/private$/queue Name
Note: The FormatName must be written in exactly the red notation! "Direct" can be arbitrary.
2) By default, the queue receives all of the information for that group. Sometimes we need to cross the domain but specify that a computer receives certain information (for example, N-units have a ... N-numbered computers are connected to a server queue, but different computers have unique numbers, and the information I send is sent only to specific computer numbers. Then you can use the label of System.message (which must refer to the class library before use), and the Code is as follows (including deleting the message after it is received to prevent more and more things in the queue):
This case is run on my own computer, the interface on the "Send" button and 2 text boxes (accept the message label 1 and 2, respectively), to prevent the time to demonstrate too long, using the asynchronous mechanism.
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.messaging;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMSG =System.Messaging.Message;namespacemessagequeuedemo{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private Asynctask<string> GetMessage (stringlabelid) {List<Msg> msgs =NewList<msg>(); //listen to the private queue on your current computerMessageQueue Msgqueue =NewMessageQueue (@". \private$\myqueue"); stringresult =NULL; Random R=NewRandom (); awaitTask.run (() = { //Use this method to allow you to dynamically delete a queue varme =Msgqueue.getmessageenumerator2 (); MSG tmpmsg=NULL; for(IEnumerator pointer =me; pointer. MoveNext ();) { //returns the current queue firstTmpmsg =(MSG) pointer. Current; //if the label matches the ID that is acceptable to the current text box, delete the MSG if(Tmpmsg.label = =labelid) {tmpmsg=me. Removecurrent (); //analog receive time is differentThread.Sleep (R.next ( -, -)); //You must specify the storage information format for each MSGTmpmsg.formatter =NewXMLMessageFormatter (NewType[] {typeof(string) }); Msgs. ADD (TMPMSG); //must be added, since deleting the current MSG does not use "reset", which causes MoveNext to be false. me. Reset (); } } if(Msgs. Count >0) {result=string. Join (",", ( fromMinchmsgsSelectm.body.tostring ())); } }); returnresult; } Private Async voidButton1_Click (Objectsender, EventArgs e) { //initialize a queue that has already been createdMessageQueue Msgqueue =NewMessageQueue (@". \private$\myqueue"); //Analog Send MSG (1 and 2 alternately sent) for(inti =1; I < One; i++) {MSG message=NewMSG (i%2==0?"1":"2",NewXMLMessageFormatter (NewType[] {typeof(string) }));
Send first parameter: The message object being sent; the second argument: Label
Msgqueue.send (message, I % 2 = = 0 " 1 " : " 2 "
} // asynchronous receive var await GetMessage ("1"); var await GetMessage ("2"); = content1; = Content2;}} }
Message Queue Basic Usage instructions