Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hello

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:

C#
private bool HasMessage()
{
   return _MessageQueue.GetAllMessages().Count() > 0;
}


I'd appreciate if someone know a easy way to get this
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jun-13 15:27pm    
Why? This is the whole point.
—SA
Jeankininho 5-Jun-13 15:35pm    
My code is: while(HasMessage()); So while there are message in Queue...
Sergey Alexandrovich Kryukov 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.
—SA
Jeankininho 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 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.
—SA

Such property is not available, due to the multithreading nature of the class System.Messaging.MessageQueue:
http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.aspx[^].

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: http://msdn.microsoft.com/en-us/library/t5te2tk0.aspx[^].

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: http://msdn.microsoft.com/en-us/library/829zyck7.aspx[^].

—SA
 
Share this answer
 
Comments
Prasad Khandekar 5-Jun-13 15:38pm    
Yes, definitely agree with you on this. +5
Sergey Alexandrovich Kryukov 5-Jun-13 15:51pm    
Thank you, Prasad.
—SA
Prasad Khandekar 5-Jun-13 15:56pm    
Will this MSDN article (http://msdn.microsoft.com/en-us/library/hce32dze(v=vs.71).aspx) provide some guidelines to Jeankininho in implementing it correctly?
Sergey Alexandrovich Kryukov 5-Jun-13 15:57pm    
Probably, a good idea to look at it; thank you for the note.
—SA
Hello,

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

Regards,
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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.

—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900