消息队列循环

发布于 2024-10-23 17:45:49 字数 191 浏览 5 评论 0原文

我有一个 C# 控制台应用程序,我试图从消息队列接收消息,直到消息队列为空。比如,

MessageQueue queue = new MessageQueue();
While(queue.notempty)
{
    queue.receive(...)
}

我该怎么做?

谢谢。

I have a console app in c# and I am trying to receive a message from a message queue until the message queue is empty. So something like,

MessageQueue queue = new MessageQueue();
While(queue.notempty)
{
    queue.receive(...)
}

How can I do this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

上课铃就是安魂曲 2024-10-30 17:45:49

无法检查队列是否为空。你必须以这种方式接收它:

private Message Receive(MessageQueue queue)
{
   try 
   {
      return queue.Receive(TimeSpan.Zero);
   }
   catch (MessageQueueException mqe)
   {
      if (mqe.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
         return null;
      throw;
   }
}

然后你可以做

MessageQueue queue = new MessageQueue();
Message msg;
while( (msg = Receive(queue)) != null)
{
    //process
}

There is no way to check whether queue is empty or not. You have to receive it this way:

private Message Receive(MessageQueue queue)
{
   try 
   {
      return queue.Receive(TimeSpan.Zero);
   }
   catch (MessageQueueException mqe)
   {
      if (mqe.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
         return null;
      throw;
   }
}

then you can do

MessageQueue queue = new MessageQueue();
Message msg;
while( (msg = Receive(queue)) != null)
{
    //process
}
可是我不能没有你 2024-10-30 17:45:49

如果您在 C# 中执行此操作,Queue类应该可以工作:

Queue<Message> queue = GetMessageQueue();

while(queue.Count > 0)
{
     Message message = queue.Dequeue();
     // process message
}

如果您计划从多个线程填充队列,您可以考虑使用 ConcurrentQueue(或 BlockingCollection)。


如果您使用 MSMQ 的 MessageQueue 类,则总是可以使用:

MessageQueue queue = ...

TimeSpan timeout = TimeSpan.FromMilliseconds(100); // Small timeout - can be very small

Message message = queue.Receive(timeout);
while (message != null)
{
      // Process message...


      message = queue.Receive(timeout);
}

If you're doing this in C#, the Queue<T> class should work:

Queue<Message> queue = GetMessageQueue();

while(queue.Count > 0)
{
     Message message = queue.Dequeue();
     // process message
}

If you plan to populate the queue from multiple threads, you could consider using ConcurrentQueue<T> (or BlockingCollection<T>) instead.


If you are using the MessageQueue class for MSMQ, you can always use:

MessageQueue queue = ...

TimeSpan timeout = TimeSpan.FromMilliseconds(100); // Small timeout - can be very small

Message message = queue.Receive(timeout);
while (message != null)
{
      // Process message...


      message = queue.Receive(timeout);
}
紫罗兰の梦幻 2024-10-30 17:45:49
 public class PersonDetails
 {
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public DateTime DateTime { get; set; }
 }

     const string queueName = @".\private$\PersonQueue";

     public void GetMessageFromQueue(string queueName)
     {
      MessageQueue perosnMessageQueue = new MessageQueue(queueName);
     try
     {
     XmlMessageFormatter xmlMessageFormatter = new XmlMessageFormatter(new Type[] { 
                               (typeof(PersonDetails)) });
      perosnMessageQueue.Formatter = xmlMessageFormatter;
      perosnMessageQueue.Refresh();
      for (int i = perosnMessageQueue.GetAllMessages().Count(); i !=0; i--)
      {
      var personDetailsFromQueue =
                         (PersonDetails)perosnMessageQueue.Receive
                         (MessageQueueTransactionType.Automatic).Body;

        Console.WriteLine("FistName : {0} \n LastName : {1} \n Date Time : 
         {2}",personDetailsFromQueue.FirstName, 
              personDetailsFromQueue.LastName, 
              personDetailsFromQueue.DateTime);`  
       }
 public class PersonDetails
 {
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public DateTime DateTime { get; set; }
 }

     const string queueName = @".\private$\PersonQueue";

     public void GetMessageFromQueue(string queueName)
     {
      MessageQueue perosnMessageQueue = new MessageQueue(queueName);
     try
     {
     XmlMessageFormatter xmlMessageFormatter = new XmlMessageFormatter(new Type[] { 
                               (typeof(PersonDetails)) });
      perosnMessageQueue.Formatter = xmlMessageFormatter;
      perosnMessageQueue.Refresh();
      for (int i = perosnMessageQueue.GetAllMessages().Count(); i !=0; i--)
      {
      var personDetailsFromQueue =
                         (PersonDetails)perosnMessageQueue.Receive
                         (MessageQueueTransactionType.Automatic).Body;

        Console.WriteLine("FistName : {0} \n LastName : {1} \n Date Time : 
         {2}",personDetailsFromQueue.FirstName, 
              personDetailsFromQueue.LastName, 
              personDetailsFromQueue.DateTime);`  
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文