消息队列异步处理
消息队列异步处理
作者:飞鸟 | 出处:博客园 | 2011/9/19 5:07:02 | 阅读22次
private System.Messaging.MessageQueue mqAsy;
private void AsyReceive()
{
if(MessageQueue.Exists(@".\myQueue"))
//creates an instance MessageQueue, which points to the already existing MyQueue
mqAsy = new System.Messaging.MessageQueue(@".\myQueue");
else
//creates a new private queue called MyQueue
mqAsy = MessageQueue.Create(@".\myQueue");
mqAsy.ReceiveCompleted+=new ReceiveCompletedEventHandler(mqAsy_ReceiveCompleted);
mqAsy.BeginReceive(new TimeSpan(0,0,1));
}
private void mqAsy_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
System.Messaging.Message m= mqAsy.EndReceive(e.AsyncResult);
m.Formatter = new XmlMessageFormatter(new String[]{"System.String,mscorlib"});
if(m.Body.ToString()!=null)
{
// input your code here
}
mqAsy.BeginReceive(new TimeSpan(0,0,1));
}
private void AsyReceive()
{
if(MessageQueue.Exists(@".\myQueue"))
//creates an instance MessageQueue, which points to the already existing MyQueue
mqAsy = new System.Messaging.MessageQueue(@".\myQueue");
else
//creates a new private queue called MyQueue
mqAsy = MessageQueue.Create(@".\myQueue");
mqAsy.ReceiveCompleted+=new ReceiveCompletedEventHandler(mqAsy_ReceiveCompleted);
mqAsy.BeginReceive(new TimeSpan(0,0,1));
}
private void mqAsy_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
System.Messaging.Message m= mqAsy.EndReceive(e.AsyncResult);
m.Formatter = new XmlMessageFormatter(new String[]{"System.String,mscorlib"});
if(m.Body.ToString()!=null)
{
// input your code here
}
mqAsy.BeginReceive(new TimeSpan(0,0,1));
}