日期:2014-05-18 浏览次数:21200 次
/// <summary> /// 利用系统message构造的消息队列,可在不同的进程之间共享队列 /// </summary> /// <typeparam name="T"></typeparam> public class MSMQueue<T> { private MessageQueue _queue; private bool _noBlockThread = false; private string _queuePath = ""; private int _dequeueThreadCount = 0; /// <summary> /// 是否不阻塞线程 /// </summary> public bool NoBlockThread { get { return _noBlockThread; } } internal MessageQueue BaseQueue { get { return _queue; } } private const string EMTPYMESSAGE = ""; private string _queueName = ""; /// <summary> /// 队列名称 /// </summary> public string QueueName { get { if (string.IsNullOrEmpty(_queueName)) { _queueName = _queue.QueueName.Split('\\')[1]; } return _queueName; } } public MSMQueue(string queuePath) { bool isExists = CreateMessageQueue(queuePath); if (isExists) { RepairCount(); } } private const string _queueNamePrefix = @"private$\"; /// <summary> /// 构造已知msmq队列 /// </summary> /// <param name="queue"></param> internal MSMQueue(MessageQueue queue) { _queue = queue; _queuePath = string.Concat(@".\",_queueNamePrefix, QueueName); //Count属性中会去修正InnerCount值 RepairCount(); } internal MSMQueue(MessageQueue queue, int dequeueThreadCount) { _queue = queue; _queuePath = string.Concat(@".\", _queueNamePrefix, QueueName); _dequeueThreadCount = dequeueThreadCount; _noBlockThread = true; RestoreCount(); //Count属性中会去修正InnerCount值 RepairCount(); } public MSMQueue(string queuePath, int dequeueThreadCount) { if (dequeueThreadCount <= 0) { throw new ArgumentException("获取消息的线程数必须大于0!"); } //当队列中没有可用消息时,不阻塞线程 _noBlockThread = true; _dequeueThreadCount = dequeueThreadCount; bool isExis