2
Answers

A Question About MSMQ(Microsoft Message Queue) in .net 2.0

Steven

Steven

16y
8.1k
1

When the code below executes,the btnSendMessage_Click method(Send Message) can complete normally.
But the btnReceiveMessage_Click method blocks at 'queue.Receive();' without any exception.Why can't
receive message?Do I need to configure my OS(Windows XP) yet ?
Any help will be appreciated!

// Send Message
private void btnSendMessage_Click(object sender, System.EventArgs e)
{

// Open queue
System.Messaging.MessageQueue queue =
new System.Messaging.MessageQueue(".\\Private$\\MSMQDemo");

// Create message
System.Messaging.Message message = new System.Messaging.Message();

message.Body = txtMessage.Text.Trim();

message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(string)});

// Put message into queue
queue.Send(message);

}



// Receive Message
private void btnReceiveMessage_Click(object sender, System.EventArgs e)
{

// Open queue
System.Messaging.MessageQueue queue =
new System.Messaging.MessageQueue(".\\Private$\\MSMQDemo");


// Receive message
System.Messaging.Message message = queue.Receive();

message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(string)});

txtReceiveMessage.Text = message.Body.ToString();
}
Answers (2)
0
Steven

Steven

NA 4 0 16y

Hi,Sateesh Kumar
Thank you for so quick reply.
To my surprise,when I use the Computer Management tool in Windows XP,the message sent doesn't exist in the queue.
But the process of sending message doesn't result in an exception!Why,why?

0
Sateesh Arveti

Sateesh Arveti

NA 24k 6.2m 16y

Hi,

Receive method gets a message from the queue,if exists else it waits for a message to appear .So it will be blocked,if no message exists in the Queue.You can set Timeout for Message Waiting.