.NET's MSMQ asynchronous calls Shanhe [original]

zhaozj2021-02-16  72

1. Reference system.Messagiong.dll2, use system.Messagiong Namespace 3, create / build your own queue (here monitoring exclusive queue) String Queuename = ".///prate $ // Testqueue"; MessageQueue q; MessageQueue.exists (queuename)) {q = new system.Messaging.MessageQueue (queuename);} Else {= messagequeue.create (queuename);} This code is executed, it will see the proprietary queue in the message queue A queue called TestQueue. Specifically Administrative Tools -> Computer Management -> Services and Applications -> Message Queuing -> Dedicated Queues You can see messages existing in the queue 4, send messages to queues to send messages: simple messages, Directly use Q.send ("...."); manner, complex messages (such as a specific object, of course, can be serialized), using Message M = New Message (); M.Label = "MSG1"; m.body = "test body"; q.send (m); send. The message will be sent to the queue in the default XML format encoding 5. The message receives the active mode, which will synchronize the current thread MESSAGE M = Receive (); // Remove the message or Message M = peek from the queue or Message M = peek (); / / Do not delete the received message passive mode, asynchronous call mode, will not block the current thread Q.ReceiveCompleted = new receiveCompletedEventHandler (); // event q.beginReceive (); where ReceiveDevt either a static function, or must be examples of objects that belong to the access function private static void ReceivedEvt (object source, ReceiveCompletedEventArgs asyncResult) {try {MessageQueue mq = (MessageQueue) source; Message m = mq.EndReceive (asyncResult.AsyncResult); // m is an asynchronous matter The received message // This inserted the code console.Writeline ("Receive message" m.Label); mq.beginReceive (); // Receive the next event} catCH (MessageQueeexception) {} return;

转载请注明原文地址:https://www.9cbs.com/read-15061.html

New Post(0)