Microsoft Exam Questions

What should you do?

You are writing an application that reads messages from a message queue.
The name of the message queue is stored in a member variable named queueName.
When a message is read, the application processes the message.
The code for the application is as follows:

class MyApp
{
MessageQueue queue;
public MyApp()
{
queue = new MessageQueue(queueName, QueueAccessMode.Receive);
queue.ReceiveCompleted += new ReceiveCompletedEventHandler(this.ReceivedMessage);
queue.BeginReceive();
}
bool KeepListening() { }
void ProcessMessage(Message m) { }
}

You need to ensure that the application continues to read messages when the KeepListening method returns True, and stops when the KeepListening method returns False.

What should you do?

A.
Use the following implementation of the ReceivedMessage method.
private void ReceivedMessage(object sender, ReceiveCompletedEventArgs e)
{
ProcessMessage(queue.EndReceive(e.AsyncResult));
if (KeepListening() == true)
{
queue.BeginReceive();
}
}

B.
Use the following implementation of the ReceivedMessage method.
private void ReceivedMessage(object sender, ReceiveCompletedEventArgs e)
{
queue = new MessageQueue(queueName, QueueAccessMode.Receive);
ProcessMessage(queue.EndReceive(e.AsyncResult));
if (KeepListening() == true)
{
queue.BeginReceive();
}
}

C.
Use the following implementation of the ReceivedMessage method.
private void ReceivedMessage(object sender, ReceiveCompletedEventArgs e)
{
queue = new MessageQueue(queueName,
QueueAccessMode.Receive);
ProcessMessage(queue.EndReceive(e.AsyncResult));
if (KeepListening() == false)
{
queue.ReceiveCompleted -= new ReceiveCompletedEventHandle(this. ReceivedMessage);
}
}

D.
Use the following implementation of the ReceivedMessage method.
private void ReceivedMessage(object sender, ReceiveCompletedEventArgs e)
{
ProcessMessage(queue.EndReceive(e.AsyncResult));
if (KeepListening() == false)
{
queue.ReceiveCompleted -= new ReceiveCompletedEventHandler(this.ReceivedMessage);
}
}