Click here to Skip to main content
11,806,350 members (64,801 online)
Rate this: bad
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 9:02am
Sergey Alexandrovich Kryukov at 5-Jun-13 15:27pm
Why? This is the whole point.
Jeankininho at 5-Jun-13 15:35pm
My code is: while(HasMessage()); So while there are message in Queue...
Sergey Alexandrovich Kryukov at 5-Jun-13 15:39pm
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 at 5-Jun-13 15:45pm
I just want know:
There are a way to know if there are Messages on the queue? Simple.
Sergey Alexandrovich Kryukov at 5-Jun-13 15:56pm
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 at 5-Jun-13 15:38pm
Yes, definitely agree with you on this. +5
Sergey Alexandrovich Kryukov at 5-Jun-13 15:51pm
Thank you, Prasad.
Prasad Khandekar at 5-Jun-13 15:56pm
Will this MSDN article ( provide some guidelines to Jeankininho in implementing it correctly?
Sergey Alexandrovich Kryukov at 5-Jun-13 15:57pm
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.

Sergey Alexandrovich Kryukov at 5-Jun-13 15:35pm
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
0 Richard Deeming 205
1 Sergey Alexandrovich Kryukov 170
2 ppolymorphe 145
3 OriginalGriff 135
4 Abhinav S 120
0 OriginalGriff 3,330
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,905
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.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