MSMQ processing of a string
Here is some sample code that processes messages that are in an MSMQ queue (isn't that redundant?). It pulls a string from a defined queue and does something with it. Behind the scenes, it uses the threadpool to process messages in the queue.
//do a bunch of setup stuff.
//this code could be within a winforms button click, form load, or the start of a Windows Service.
gMQSearchUrl = new MessageQueue();
gMQSearchUrl.Path = gstrQueueForSearchUrl;
gMQSearchUrl.Formatter = new XmlMessageFormatter(new string[]{"System.String, mscorlib"});
gMQSearchUrl.ReceiveCompleted += new ReceiveCompletedEventHandler(this.MQ_ReceiveCompleted_ForSearchUrl );
gMQSearchUrl.BeginReceive();
private void MQ_ReceiveCompleted_ForSearchUrl(object sender, ReceiveCompletedEventArgs e)
{
// Add code here to respond to message.
//new XmlMessageFormatter(new String(){"System.String, mscorlib"});
System.Messaging.Message msg = gMQSearchUrl.EndReceive(e.AsyncResult);
msg.Formatter = new XmlMessageFormatter(new String[]{"System.String, mscorlib"});
string strBody = (string)msg.Body;
try
{
//Do something with the message that is stored in strBody.
}
//Exception handling code
finally
{
if ( this.gblRunStatus == true )
{
//You have to recieve the next message.
gMQSearchUrl.BeginReceive();
}
msg.Dispose();
msg = null;
}
}