简体   繁体   English

如何使用队列发送数据?

[英]How do I use queues to send data?

If I was to, from various sources, put data in a queue, and whenever it has anything to send, it sends it and then waits for a time. 如果要从各种来源将数据放入队列中,并且每当有数据要发送时,它都会将其发送,然后等待一段时间。 So, for example, the queue is empty, I send in 2 things to send, it sends the first thing instantly and then waits for a bit before sending the next. 因此,例如,队列为空,我发送了两个要发送的东西,它立即发送了第一件事,然后等待一会儿再发送下一个。 How would I do this? 我该怎么做?

Sounds like you should have a producer/consumer queue, with a single consumer which delays after consuming. 听起来您应该有一个生产者/消费者队列,只有一个消费者,消费后会延迟。 (The consumer would be on a different thread to the producers, of course.) (当然,消费者将与生产者处于不同的线程。)

From .NET 4 onwards, BlockingCollection<T> is the fundamental building block of producer/consumer queues. 从.NET 4开始, BlockingCollection<T>是生产者/消费者队列的基本构建块。 You should also look at TPL DataFlow though - it may have a simple way of doing this out of the box. 但是,您还应该查看TPL DataFlow-它可能具有一种开箱即用的简单方法。 (DataFlow is a library specifically aimed at producer/consumer scenarios.) (DataFlow是专门针对生产者/消费者方案的库。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM