0

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.

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • What is *component* (could be [this](http://msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx))? Class? How do you use this class? Simplest approach to pass data is to use event and wrap data into `EvengArgs`. But it could be also good to have *argumentless* events (when you retrive data from `sender` or from instance, which is accessible somehow). – Sinatr May 12 '14 at 10:06
  • Would events not serve for this purpose? – Paulie Waulie May 12 '14 at 10:07
  • @Sinatr component is just a theoretical name I chose to represent the application on a higher level. – Zapnologica May 12 '14 at 10:07
  • The blocking approach is traditional in a Producer/Consumer scheme, use a BlockingCollection (wrapper). – H H May 12 '14 at 10:16
  • This way whenever data is added it can just go fetch it' - how would it know to go fetch it? To fire an event on a consumer thread, the consumer would require an event-dispatching loop, ie. it would have to wait on a blocking queue for messages..... – Martin James May 12 '14 at 11:55

2 Answers2

2

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.

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
  • Ok so basically what you are saying, Unless I want to get heavily complicated blocking is a sufficient solution? – Zapnologica May 12 '14 at 10:25
2

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.

S.N
  • 4,910
  • 5
  • 31
  • 51
  • Yes you are correct, Multiple producers single consumer – Zapnologica May 12 '14 at 10:39
  • 1
    @Zapnologica, To complete, you can go with option 2, where encapsulate the a custom eventing in you consumer 1 implementation, where an event will be fired when an item is added into a queue. And then subscribe for this event in your Component 2. Hope this help. – S.N May 12 '14 at 11:04