C #. Net simple application example of WINDOWS Message Queue
1. Install the message queue on the machine.
2. Start a program for listening to the Message Queue. The program keeps Retrieving messages from the message queue and then processes them. The core code of the program is as follows:
// Initialize the Message Queue <br/> // define a Message Queue variable <br/> messagequeue mymsmq; <br/> string strmsmqpath = "Message Queue name (. \ private $ \ cnkiflashviewer) "; <br/> If (messagequeue. exists (strmsmqpath) <br/>{< br/> This. mymsmq = new messagequeue (strmsmqpath); <br/>}< br/> else <br/>{< br/> This. mymsmq = messagequeue. create (strmsmqpath); <br/>}< br/> This. mymsmq. synchronizingobject = This; <br/> This. mymsmq. formatter = new xmlmessageformatter (New Type [] {typeof (msmqorder )}); </P> <p> // start a thread <br/> thread TRD = new thread (this. trd_task); <br/> TRD. isbackground = true; <br/> TRD. start (); </P> <p> // thread execution function <br/> static object o = new object (); <br/> // The thread processes the message entity. <br/> private void trd_task () <br/>{< br/> lock (o) <br/>{< br/> while (true) <br/>{< br/> // flag_stop indicates whether to stop <br/> If (this. flag_stop) <br/> break; <br/> // order is a defined class that stores the message entity <br/> order = NULL; <br/> This. mutex_msmq.waitone (); <br/> try <br/>{< br/> system. messaging. message MSG = This. mymsmq. receive (New timespan (0, 0, 1); <br/> order = (Order) MSG. body; <br/>}< br/> catch <br/>{}< br/> This. mutex_msmq.releasemutex (); <br/> If (Order = NULL) <br/> continue; <br/> timespan Ts = datetime. now-order. ordertime; <br/> // determines whether the request has timed out. <br/> If (TS. totalseconds> This. timeout) <br/>{< br/> continue; <br/>}< br/> try <br/>{< br/> // perform business processing <br/>}< br/> catch <br/> {} <br/>}< br/> # endregion <br/> // message entity <br/> [serializable] <br/> public class order <br/>{< br/> Public int type {Get; set ;}< br/> Public String URL {Get; Set ;}/< br/> Public datetime ordertime; <br/>}
3. Message Queue sending Method
Public void sendmsmq (string strurl) <br/>{< br/> string strmsmqpath = ". \ private $ \ cnkiflashviewer "; <br/> messagequeue MQ = NULL; <br/> If (messagequeue. exists (strmsmqpath) <br/>{< br/> MQ = new messagequeue (strmsmqpath ); <br/>}< br/> else <br/> {<br/> // you can create <br/> // MQ = messagequeue. create (strmsmqpath); <br/> return; <br/>}< br/> MQ. formatter = new xmlmessageformatter (New Type [] {typeof (msmqorder)}); <B R/> messageenumerator enumerator = MQ. getmessageenumerator2 (); <br/> bool isexist = false; <br/> while (enumerator. movenext () <br/>{< br/> message MSG = (Message) (enumerator. current); <br/> msmqorder temp = (msmqorder) (MSG. body); <br/> If (temp. url = strurl) <br/>{< br/> isexist = true; <br/> break; <br/>}< br/> If (! Isexist) <br/>{< br/> msmqorder msoder = new msmqorder (); <br/> msoder. type = 1; <br/> msoder. url = strurl; <br/> msoder. ordertime = datetime. now; <br/> MQ. send (msoder); <br/>}< br/>}
4. Application Message Queue
Add sendurl (strurl) to the code of the message queue to be sent. Then, the program waits for a certain amount of time to verify the message execution result.
// Send the message <br/> sendurl (strurl); <br/> // wait for 30 seconds <br/> int retrytimers = 30; <br/> string strfilenamefullpath = @ "D: \ 3.doc"; <br/> for (INT I = 0; I <retrytimers; I ++) <br/>{< br/> thread. sleep (1000); <br/> // verify whether a file is generated <br/> If (filehelper. isexist (strfilenamefullpath, false) <br/>{< br/> // prompt for file generation or other business processing <br/> break; <br/>}< br/> else <br/>{< br/> if (I <retrytimers-1) <br/> continue; <br/> else <br // >{< Br/> // prompt that the generation fails and jumps to other code. <Br/> // other services <br/>}< br/>}