问题
I am writing an application that peeks message from Microsoft Message Queue (MSMQ). I want my application peeks MSMQ messages in turn(from the first message to the last message ). After peeks the last message completly, the main thread will be blocked until MSMQ have a new message arrive.
I've written a bit of code, but have an exception that explained like below:
Message ret = null;
Cursor cursor = mq.CreateCursor();
bool isTheFirst = true;
while (true) {
if (isTheFirst) {
ret = mq.Peek(new TimeSpan(Timeout.Infinite), cursor, PeekAction.Current);
Console.WriteLine(ret.Id);
isTheFirst = false;
} else {
// after my app peeks the last message completly, the peek method
//throw an exception: "Timeout is expired!"
ret = mq.Peek(new TimeSpan(Timeout.Infinite), cursor, PeekAction.Next);
Console.WriteLine(ret.Id);
}
Thread.Sleep(1000);
}
Could anyone help me to solve this problem. Thank you!
回答1:
You should use MessageQueue.InfiniteTimeout
instead of Timeout.infinite
. Try change it to this value
ret = mq.Peek(MessageQueue.InfiniteTimeout, cursor, PeekAction.Current);
The values are not the same
var timeout = MessageQueue.InfiniteTimeout; // {49.17:02:17.2950000}
var timeout2 = new TimeSpan(Timeout.Infinite); // {-00:00:00.0000001}
So the queue Peek
behaves differently
来源:https://stackoverflow.com/questions/29117142/peek-msmq-message-with-infinite-timeout