public MessageDelivery Dequeue(string id, TimeSpan timeout)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("MsmqMessageDeliveryQueue");
            }

            try
            {
                using (System.Messaging.MessageEnumerator msgEnum = _queue.GetMessageEnumerator2())
                {
                    if (msgEnum.Current.Label == id)
                    {
                        Message message = _queue.ReceiveById(msgEnum.Current.Id, _transactionType);
                        return(createObject(message));
                    }
                }

                return(null);
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    throw new TimeoutException("The timeout was reached before the specified message could be returned.");
                }
                else if (ex.MessageQueueErrorCode == MessageQueueErrorCode.MessageNotFound)
                {
                    return(default(MessageDelivery));
                }
                throw;
            }
        }
		protected override void ReceiveMessage(MessageEnumerator enumerator, TimeSpan timeout, Action<Func<Message>> receiveAction)
		{
			receiveAction(() =>
				{
					if (_log.IsDebugEnabled)
						_log.DebugFormat("Removing message {0} from queue {1}", enumerator.Current.Id, Address);

					return enumerator.RemoveCurrent(timeout, MessageQueueTransactionType.Automatic);
				});
		}
        /// <summary>
        /// Returns number of message's in message queue
        /// </summary>
        /// <param name="queue">MessageQueue : queue</param>
        /// <returns>integer</returns>
        private int GetMessageCount(MessageQueue queue)
        {
            int totalCount = 0;

            try
            {
                System.Messaging.MessageEnumerator me = queue.GetMessageEnumerator2();
                while (me.MoveNext(new TimeSpan(0, 0, 0)))
                {
                    totalCount++;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return(totalCount);
        }
        protected override void ReceiveMessage(MessageEnumerator enumerator, TimeSpan timeout,
                                               Action<Message> receiveAction)
        {
            var options = new TransactionOptions
                {
                    IsolationLevel = _isolationLevel,
                    Timeout = _transactionTimeout,
                };

            using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
            {
                using (Message message = enumerator.RemoveCurrent(timeout, MessageQueueTransactionType.Automatic))
                {
                    receiveAction(message);
                }

                scope.Complete();
            }
        }
		protected virtual void ReceiveMessage(MessageEnumerator enumerator, TimeSpan timeout,
		                                      Action<Func<Message>> receiveAction)
		{
			receiveAction(() => enumerator.RemoveCurrent(timeout, MessageQueueTransactionType.None));
		}
 protected virtual void ReceiveMessage(MessageEnumerator enumerator, TimeSpan timeout,
     Action<Message> receiveAction)
 {
     using (Message message = enumerator.RemoveCurrent(timeout, MessageQueueTransactionType.None))
     {
         receiveAction(message);
     }
 }