Click here to Skip to main content
11,929,675 members (56,065 online)
Rate this:
Please Sign up or sign in to vote.

I'm working with MessageQueue and I'd like to know if there a way to know if there are message on Queue or not. I'm using it, but is too slow:

private bool HasMessage()
   return _MessageQueue.GetAllMessages().Count() > 0;

I'd appreciate if someone know a easy way to get this
Posted 5-Jun-13 10:02am
Why? This is the whole point.
Jeankininho 5-Jun-13 15:35pm
My code is: while(HasMessage()); So while there are message in Queue...
Wrong approach! This is not how threading is reasonably uses. This is called "spin wait" and is a big abuse.
Please see my answer. If you need more help, explain the detail of your architecture, starting from you goal.
Jeankininho 5-Jun-13 15:45pm
I just want know:
There are a way to know if there are Messages on the queue? Simple.
No. You are trying to abuse the code design, and I'm just trying to help you to overcome it; everything else is up to you.
And thank you for accepting my answer, I really think it should help you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Such property is not available, due to the multithreading nature of the class System.Messaging.MessageQueue:[^].

You can thing of it as of some full-proof feature. Otherwise, what does it mean if you check up availability of the message at some moment of time and assign the result to, say, some Boolean or integer ("count") variable/member? During this operation, the only available message could be removed by some other thread, or some message could be added to the previously empty queue. Even if the thread synchronization operation is used on the queue operations, it can happen during assignment operations or somewhere else.

You can get the equivalent effect if you simply peek a message (that is, get without removing from the queue) with some small timeout:[^].

But this is not a really correct approach. Instead, you should review your code design the way not using message count at all. The technique is designed to be used in separate thread, which perform blocking calls, and, in particular, are put to the wait state unconditionally, until a message becomes available. This is that very main blocking operation:[^].

Prasad Khandekar 5-Jun-13 15:38pm
Yes, definitely agree with you on this. +5
Thank you, Prasad.
Prasad Khandekar 5-Jun-13 15:56pm
Will this MSDN article ( provide some guidelines to Jeankininho in implementing it correctly?
Probably, a good idea to look at it; thank you for the note.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Have a look at this documentation on MSDN. I think this is what you are looking for.

It won't answer the question, because this call is blocking.
However, the thing is: this is what OP should really be doing. In other words, the code design should be reviewed the way not using information of the queue item count.

I actually answered in detail, please see Solution 2.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 5 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100