消息队列异步处理

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));
  }

转载于:https://www.cnblogs.com/fellowcheng/articles/152369.html

你可能感兴趣的:(消息队列异步处理)