Question

I have a program of mine which makes use of the c# concurrent Queue to pass data from my one component to the other.

Component 1: Multiple network connections receive data and then put it into this Queue

Component 2: Reads data from this queue and then processes it.

Ok, good all makes sense ( I sure hope).

Now what I want to know, is what is the best / most efficient way to go about passing the data between the two components?

Option 1: Poll the queue for new data in component 2? Which will entail blocking code, or atleast a while(true)

Option 2: I don't know if this is possible, but that's why im here asking. Does the queue data structure not have a sort of functionality that say my component 2 can register to the queue to be notified of any inserts / changes? This way whenever data is added it can just go fetch it, and I can then avoid any blocking / polling code.

Was it helpful?

Solution

Component 1 ( Producer) require either manual or automatic blocking since you anticipate multiple access (multiple post mentioned) while producing. This means BlockingQueue make sense in Component1. However, in Component 2 (Consumer), if you think you only (at any time) have one consumer then you don’t need any blocking code.

In order to save or avoid while, you must need a mechanism to inform the consumer that someone has added something into the queue. This can be achieved using a custom eventing (not talking about EventHandle subtypes). Keep in mind, you may not have the element order in such style of eventing.

OTHER TIPS

For a simple implementation of Producer/Consumer you can try using BlockingCollection. For a more complex consumption of data from from various sources Reactive Extensions might help. It's a much steeper learning curve but it is a very powerful pull based framework, so you don't need to do any polling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top