检查C#中的MessageQueue中是否有消息? [英] Check if there are Messages in MessageQueue in C#?

查看:428
本文介绍了检查C#中的MessageQueue中是否有消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我正在使用MessageQueue,我想知道是否有办法知道Queue上是否有消息。我正在使用它,但速度太慢了:



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

推荐答案

由于类的多线程性质 System.Messaging.MessageQueue

<此类属性不可用a href =http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.aspx> http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue .aspx [ ^ ]。



你可以把它作为一些全面证明的功能。否则,如果您在某个时刻检查消息的可用性并将结果分配给某些布尔或整数(count)变量/成员,这意味着什么?在此操作期间,可以通过某个其他线程删除唯一可用的消息,或者可以将某些消息添加到先前空的队列中。即使在队列操作中使用线程同步操作,也可能在赋值操作期间或其他地方发生。



如果你只是 peek 一条消息(即没有从队列中删除),带有一些小的超时 http://msdn.microsoft.com/en-us/library/t5te2tk0.aspx [ ^ ]。



但这不是一个非常正确的方法。相反,您应该以不使用消息计数的方式检查您的代码设计。该技术设计用于单独的线程,它执行阻塞调用,特别是无条件地进入等待状态,直到消息可用。这是非常主要的阻止操作: http://msdn.microsoft.com/en-us/library/ 829zyck7.aspx [ ^ ]。



-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


你好,



在MSDN上查看文档。我想这就是你要找的。



问候,
Hello,

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

Regards,


这篇关于检查C#中的MessageQueue中是否有消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆