Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Win2008-R2 Message
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:
 
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
Comments
Sergey Alexandrovich Kryukov at 5-Jun-13 15:27pm
   
Why? This is the whole point.
—SA
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.
—SA
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.
—SA
Rate this: bad
good
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:
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
  Permalink  
Comments
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.
—SA
Prasad Khandekar at 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 at 5-Jun-13 15:57pm
   
Probably, a good idea to look at it; thank you for the note.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Have a look at this documentation on MSDN. I think this is what you are looking for.
 
Regards,
  Permalink  
Comments
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.
 
—SA

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

  Print Answers RSS
0 OriginalGriff 275
1 Maciej Los 230
2 Sergey Alexandrovich Kryukov 185
3 Afzaal Ahmad Zeeshan 152
4 ProgramFOX 130
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 5 Jun 2013
Copyright © CodeProject, 1999-2014
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