如何取得消息队列中消息总数

.NET MessageQueue 类没有取得消息队列中消息总数的API,用遍历的方式得到消息队列中消息总数在消息总数巨大的时候效率很低下,以下办法可以比较好的解决这个问题。

1. 运行环境。

Windows 2003 Server

VS2005

安装消息队列服务

2. 添加引用。

如下图在项目中添加引用。

clip_image002 

3. 方法

ExpandedBlockStart.gif ContractedBlock.gif         /**/ /// <summary>
       
/// 得到消息队列中消息个数
       
/// </summary>
       
/// <param name="machineName">消息队列机器名</param>
       
/// <param name="queueFormatName">消息队列FormatName</param>
       
/// <returns>消息队列中消息总数</returns>

        private   int GetMessageCount( string machineName, string queueFormatName)
ExpandedBlockStart.gifContractedBlock.gif       
{
            MSMQ.MSMQManagement mgmt
= new MSMQ.MSMQManagement();
            Object ss
= (Object)machineName;
            Object pn
= (Object)queueFormatName;
            Object f
= (Object)null;
            mgmt.Init(
ref ss, ref f, ref pn);
           
return mgmt.MessageCount;
        }

4. 调用

int msgCount = GetMessageCount(msgQueue.MachineName, msgQueue.FormatName);

msgQueue---内聚的MessageQueue对象

5. 限制

该方法适用于消息队列服务与应用程序都在一台电脑上时适用,二者不在一台电脑上时会抛出异常,具体原因不明。

6. 参考资料

MSDN 搜索 MSMQManagement。

转载于:https://www.cnblogs.com/zky123/archive/2009/06/06/1497424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值