home *** CD-ROM | disk | FTP | other *** search
-
- <!-- #include virtual="/quickstart/howto/include/header.inc" -->
-
- <h4>How Do I...Receive asynchronously?</h4>
-
- <div class="indent" style="width:660">
- <p style="MARGIN-TOP: 6px; MARGIN-BOTTOM: 5px; MARGIN-RIGHT: 0px"><font face="Tahoma" size="1">Message
- queuing makes it easy for application developers to communicate with application
- programs quickly and reliably by sending and receiving messages. Messaging
- provides you with guaranteed message delivery and a robust, fail-safe way to
- carry out many of your business processes.<br>
- <br>
- The MessageQueue component allows you to easily incorporate message-based
- communication into your applications. Using this component and its associated
- language features, you can send and receive messages, explore existing queues,
- create and delete queues, and perform a variety of other operations using a
- simple programming model.<br>
- <br>
- The sample illustrates how to use the MessageQueue component to watch a message
- queue for arrival of new messages. To run the sample you have to have Message
- Queuing installed on your system. The sample is a command line application that
- takes one command line argument. The argument is the name of a public message
- queue on your local machine. For example you can run it as follows:<br>
- <br>
- </font><font face="Courier New" color="#0000ff" size="2"> > MQAsync.exe
- MyQueue</font><font face="Tahoma" size="1"><br>
- <br>
- Now, use the MQSend sample to send a message
- to the MyQueue queue. The sample application
- will be notified when the message arrives at the queue and will output the
- message to the console.<br>
- </font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">In
- its simplest form, asynchronously receiving a message from a message queue
- involves:</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">1.
- Creating an instance of the MessageQueue component and setting its Path
- property:</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">MessageQueue
- mq = new MessageQueue(".\\MyQueue");</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">2.
- Setting up an event handler:</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">mq.AddOnReceiveCompleted(new
- ReceiveCompletedEventHandler(OnReceiveCompleted));</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">3.
- Inplementing an event handler:</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">public static void OnReceiveCompleted(Object source,
- ReceiveAsyncEventArgs asyncResult){<br>
- MessageQueue mq = (MessageQueue)source;<br>
- Message m = mq.EndReceive(asyncResult.AsyncResult);<br>
- Console.WriteLine("Message: " + (string)m.Body); <br>
- mq.BeginReceive();<br>
- }</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">4.
- Calling BeginReceive to start an asynchronous receive operation:</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">mq.BeginReceive();</font></p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
- <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">Please
- note that BeginReceive will receive only one message. If you want to keep
- receiving messages, you have to call BeginReceive again (see the event handler
- implementation in step 3). Have a good time MessageQueue'ing!</font></p>
-
- </div>
-
- <h4>Example</h4>
-
- <p>
- <div class="indent">
- <a target="_blank" href="/quickstart/howto/samples/Services/MessageQueue/MQAsync">
- <img style="border-color:black" border=1 src="/quickstart/images/genicon.gif"><br>
- </a>
- <div class="caption">MQAsync.exe</div><br>
- [<a target="_blank" href="/quickstart/howto/samples/Services/MessageQueue/MQAsync">View Sample</a>] |
- [<a target="_blank" href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Services/MessageQueue/MQAsync/MQAsync.src">View Source</a>]<p>
- </div>
-
- <h4>Source Code</h4>
-
- <div class="code">
- <xmp>
- <!-- #include virtual="/quickstart/howto/samples/Services/MessageQueue/MQAsync/MQAsync.cs" -->
- </xmp>
- </div>
-
- <!-- #include virtual="/quickstart/howto/include/footer.inc" -->