读取并删除 MSMQ 中的所有消息(通过 RemoveCurrent 方法)

Read and remove all messages in a MSMQ (via RemoveCurrent method)

我尝试通过 GetMessageEnumerator2 方法从 MSMQ 读取所有消息,但总是只获得 1 条消息,而使用 GetAllMessages 方法我获得了正确数量的消息。 .

下面代码中的"bug"在哪里:

//var messages = queue.GetAllMessages(); // HERE I HAVE 5 MESSAGES
var messages = queue.GetMessageEnumerator2();
ReadQueryMessages(messages, removeAfterRead: true);

//...

ReadQueryMessages(MessageEnumerator enumerator, bool removeAfterRead = true)
{
    List<Message> messages = new List<Message>();
    // enumerator.Reset(); - didn't help
    while (enumerator.MoveNext(new TimeSpan(0, 0, 1)))
    {
        messages.Add(enumerator.Current);
        if (removeAfterRead)
            enumerator.RemoveCurrent();
    }
    // process messages - HERE I HAVE ONLY 1 FIRST MESSAGE
}

PS(对于"duplicate-moderators")
我的问题不是问 HOW,而是 WHY only one when removed

我不知道为什么 RemoveCurrent 不起作用(或起作用,但会中断队列读取),但我通过添加 queue 作为参数来修复:

List<ELetter> ReadQueryMessages(MessageQueue queue, 
                                MessageEnumerator enumerator, 
                                bool removeAfterRead = true)
{
    List<Message> messages = new List<Message>();
    while (enumerator.MoveNext(new TimeSpan(0, 0, 1)))
    {
        messages.Add(enumerator.Current);
        if (removeAfterRead)
            queue.ReceiveById(enumerator.Current.Id); 
            // enumerator.RemoveCurrent(); - this does not work properly!
    }
    // here I have 5 messages in the list - OK.
}