Posted By

gaserland on 03/01/13


Tagged

MSMQ MessageQueue Counting


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

danfinrud55


Counting Messages in an MSMQ MessageQueue


 / Published in: C#
 

Counting Messages in an MSMQ MessageQueue from C#

  1. protected Message PeekWithoutTimeout(MessageQueue q, Cursor cursor, PeekAction action)
  2. {
  3. Message ret = null;
  4. try
  5. {
  6. ret = q.Peek(new TimeSpan(1), cursor, action);
  7. }
  8. catch (MessageQueueException mqe)
  9. {
  10. if (!mqe.Message.ToLower().Contains("timeout"))
  11. {
  12. throw;
  13. }
  14. }
  15. return ret;
  16. }
  17.  
  18. protected int GetMessageCount(MessageQueue q)
  19. {
  20. int count = 0;
  21. Cursor cursor = q.CreateCursor();
  22.  
  23. Message m = PeekWithoutTimeout(q, cursor, PeekAction.Current);
  24. if (m != null)
  25. {
  26. count = 1;
  27. while ((m = PeekWithoutTimeout(q, cursor, PeekAction.Next)) != null)
  28. {
  29. count++;
  30. }
  31. }
  32. return count;
  33. }

Report this snippet  

You need to login to post a comment.