Peek MSMQ message with infinite timeout

戏子无情 提交于 2019-12-11 03:08:46

问题



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!