您所在的位置: 首页 > 开发 > .Net > c# >

C#操作消息队列的代码

  • 摘要:本文简单给出了C#操作消息队列的代码,供大家参考!
  • 标签: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;
}
}

【相关文章】

【责任编辑:火凤凰 TEL:(010)68476606】

让你的代码“炫”起来——WPF开发教程
C#实用基础教程
C#技术开发指南
Scala编程语言
大型网站架构技术专家谈
 
 验证码: (点击刷新验证码)   匿名发表
  • 亮剑.NET. 图解C#开发实战

  • 作者:李新峰 付志涛 缪勇
  • 本书采用全新的图解思路,分3篇介绍使用微软C#语言开发实际应用程序的基本知识。第1篇包括10章,介绍了C#语言的基础知识,主要..
Copyright©2005-2009 51CTO.COM 版权所有