Counting Messages in an MSMQ MessageQueue


/ Published in: C#
Save to your folder(s)

Counting Messages in an MSMQ MessageQueue from C#


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.