Create a Beverage Queuing Application

Download the Queuing Warmup Exercise
- Extract to a location on your local hard drive.
- The instructor will provide you with the password.

1. Open 12b Queued Drinks.sln.
- Located in: C:\Demos\12 Networking\After\12b Queued Drinks\
- Add a reference to System.Messaging
- Add alias of MsmqMessage for System.Messaging.Message
using MsmqMessage=System.Messaging.Message;

2. In form constuctor, create queue if needed.

if (! MessageQueue.Exists(_bevQueue))
{
MessageQueue.Create(_bevQueue);
}

3. In the orderButton_Click handler send a Beverage to the queue.
- Create a message queue (in a using block)
using (MessageQueue queue = new MessageQueue(_bevQueue)) (...)

- Create a new message
MsmqMessage message = new MsmqMessage(bev);

- Send the message
queue.Send(message, bev.ToString());

4. In the consumeButton_Click event handler,
receive a Beverage from the queue (with timeout).
- Create a message queue (in a using block)
using (MessageQueue queue = new MessageQueue(_bevQueue)) (...)

- Create a new message
MsmqMessage message = new MsmqMessage(bev);

MsmqMessage message;
try
{
message = queue.Receive(TimeSpan.FromSeconds(5));
}
catch (MessageQueueException)
{
MessageBox.Show("No more beverages for now", Text);
return;
}

5. Apply a formatter (pass type array)

Type[] types = new Type[] { typeof(Beverage) };
message.Formatter = new XmlMessageFormatter(types);

6. Cast the body to a Beverage

Beverage bev = (Beverage)message.Body;

7. Add it to the list box

consumedListBox.Items.Add(bev);

8. Before running the program, view the VS Server Explorer
- Expand the Server node, then your machine node,
then Message Queues and Private Queues.

- After running the program for the first time,
you should see a private beverage queue appear.