一个关于C#操作消息队列的代码

  这是一个关于 C#操作消息队列的代码,给新手朋友学习下,并不是很难,相信大家看看就能明白的。
public class QueueManage 
  { 
  /// 
  /// 发送对象到队列中 
  /// 
  /// 
  队列名称,因为队列名称在一个应用中应该不改变的,所以大家最好写在配置文件中 
  /// 
  要发出去的对象 
  public static void SendQueue(string QueuePath,MyBase.SmsQueue sq) 
  { 
  System.Messaging.MessageQueue mqSend=new System.Messaging.MessageQueue(QueuePath,false); 
  EnsureQueueExists(QueuePath); 
  mqSend.Send(sq); 
  } 
  /// 
  /// 检查队列,如果队列不存在,则建立 
  /// 
  /// 
  队列名称 
  private static void EnsureQueueExists(string path) 
  { 
  if(!MessageQueue.Exists(path)) 
  { 
  if(!MessageQueue.Exists(path)) 
  { 
  MessageQueue.Create(path); 
  MessageQueue mqTemp=new MessageQueue(path); 
  mqTemp.SetPermissions("Everyone",System.Messaging.MessageQueueAccessRights.FullControl); 
  ///不知道该给什么样的权限好,所以就给了Everone全部权限了,当然大家最好自己控制一下 
  } 
  } 
  } 
  /// 
  /// 从队列中取出对象列表 
  /// 
  /// 
  队列名称 
  public static System.Collections.ArrayList GetMessage(string QueuePath) 
  { 
  MyBase.SmsQueue sq=new MyBase.SmsQueue(); 
  System.Messaging.MessageQueue mq=new System.Messaging.MessageQueue(QueuePath,false); 
  mq.Formatter=new XmlMessageFormatter(new Type[] {typeof(MyBase.SmsQueue)}); 
  System.Messaging.Message[] arrM=mq.GetAllMessages(); 
  mq.Close(); 
  System.Collections.ArrayList al=new System.Collections.ArrayList(); 
  foreach(System.Messaging.Message m in arrM) 
  { 
  sq=(TimeFound.SmsGate.Base.SmsQueue)m.Body; 
  al.Add(sq); 
  } 
  return al; 
  } 
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值