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!