无限超时的Peek MSMQ消息 [英] Peek MSMQ message with infinite timeout

查看:214
本文介绍了无限超时的Peek MSMQ消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在编写一个从Microsoft Message Queue(MSMQ)偷窥消息的应用程序.我希望我的应用程序依次查看MSMQ消息(从第一条消息到最后一条消息).完全查看完最后一条消息后,主线程将被阻塞,直到MSMQ有新消息到达为止.
我已经写了一些代码,但是有一个异常,解释如下:


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!

推荐答案

您应使用MessageQueue.InfiniteTimeout而不是Timeout.infinite.尝试将其更改为该值

You should use MessageQueue.InfiniteTimeout instead of Timeout.infinite. Try change it to this value

ret = mq.Peek(MessageQueue.InfiniteTimeout, cursor, PeekAction.Current);

值不相同

var timeout = MessageQueue.InfiniteTimeout; // {49.17:02:17.2950000}
var timeout2 = new TimeSpan(Timeout.Infinite); // {-00:00:00.0000001}

所以队列Peek的行为有所不同

So the queue Peek behaves differently

这篇关于无限超时的Peek MSMQ消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆