C#操作消息队列

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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;

}

}

从以前的一个项目中摘抄出来的,大家参考一下吧。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航