home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / mqasync.aspx < prev    next >
Encoding:
Text File  |  2000-06-08  |  5.2 KB  |  93 lines

  1.  
  2. <!-- #include virtual="/quickstart/howto/include/header.inc" -->
  3.  
  4. <h4>How Do I...Receive asynchronously?</h4>
  5.  
  6. <div class="indent" style="width:660">
  7. <p style="MARGIN-TOP: 6px; MARGIN-BOTTOM: 5px; MARGIN-RIGHT: 0px"><font face="Tahoma" size="1">Message
  8. queuing makes it easy for application developers to communicate with application
  9. programs quickly and reliably by sending and receiving messages. Messaging
  10. provides you with guaranteed message delivery and a robust, fail-safe way to
  11. carry out many of your business processes.<br>
  12. <br>
  13. The MessageQueue component allows you to easily incorporate message-based
  14. communication into your applications. Using this component and its associated
  15. language features, you can send and receive messages, explore existing queues,
  16. create and delete queues, and perform a variety of other operations using a
  17. simple programming model.<br>
  18. <br>
  19. The sample illustrates how to use the MessageQueue component to watch a message
  20. queue for arrival of new messages. To run the sample you have to have Message
  21. Queuing installed on your system. The sample is a command line application that
  22. takes one command line argument. The argument is the name of a public message
  23. queue on your local machine. For example you can run it as follows:<br>
  24. <br>
  25. </font><font face="Courier New" color="#0000ff" size="2">   > MQAsync.exe
  26. MyQueue</font><font face="Tahoma" size="1"><br>
  27. <br>
  28. Now, use the MQSend sample to send a message
  29. to the MyQueue queue. The sample application
  30. will be notified when the message arrives at the queue and will output the
  31. message to the console.<br>
  32. </font></p>
  33. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">In
  34. its simplest form, asynchronously receiving a message from a message queue
  35. involves:</font></p>
  36. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  37. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">1.
  38. Creating an instance of the MessageQueue component and setting its Path
  39. property:</font></p>
  40. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  41. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">MessageQueue
  42. mq = new MessageQueue(".\\MyQueue");</font></p>
  43. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  44. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">2.
  45. Setting up an event handler:</font></p>
  46. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  47. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">mq.AddOnReceiveCompleted(new
  48. ReceiveCompletedEventHandler(OnReceiveCompleted));</font></p>
  49. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  50. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">3.
  51. Inplementing an event handler:</font></p>
  52. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  53. <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,
  54. ReceiveAsyncEventArgs asyncResult){<br>
  55.     MessageQueue mq = (MessageQueue)source;<br>
  56.     Message m = mq.EndReceive(asyncResult.AsyncResult);<br>
  57.     Console.WriteLine("Message: " + (string)m.Body); <br>
  58.     mq.BeginReceive();<br>
  59.     }</font></p>
  60. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  61. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">4.
  62. Calling BeginReceive to start an asynchronous receive operation:</font></p>
  63. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  64. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Courier New" color="#0000ff" size="2">mq.BeginReceive();</font></p>
  65. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"> </p>
  66. <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; WORD-SPACING: 0px"><font face="Tahoma" size="1">Please
  67. note that BeginReceive will receive only one message. If you want to keep
  68. receiving messages, you have to call BeginReceive again (see the event handler
  69. implementation in step 3). Have a good time MessageQueue'ing!</font></p>
  70.  
  71. </div>
  72.  
  73. <h4>Example</h4>
  74.  
  75. <p>
  76. <div class="indent">
  77. <a target="_blank" href="/quickstart/howto/samples/Services/MessageQueue/MQAsync">
  78. <img style="border-color:black" border=1 src="/quickstart/images/genicon.gif"><br>
  79. </a>
  80. <div class="caption">MQAsync.exe</div><br>
  81. [<a target="_blank" href="/quickstart/howto/samples/Services/MessageQueue/MQAsync">View Sample</a>] | 
  82. [<a target="_blank" href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Services/MessageQueue/MQAsync/MQAsync.src">View Source</a>]<p>
  83. </div>
  84.  
  85. <h4>Source Code</h4>
  86.  
  87. <div class="code">
  88. <xmp>
  89. <!-- #include virtual="/quickstart/howto/samples/Services/MessageQueue/MQAsync/MQAsync.cs" -->
  90. </xmp>
  91. </div>
  92.  
  93. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->