0

Is there a way to share the same BlockingCollection across two .net applications, with one application producing new items, and the other consuming them?

The inter-process communication options I found all appear to involve serializing and deserializing the data, so if I where to pass a BlockingCollection from one process to the other, I would get a disconnected copy, and subsequent updates to the original collection would not affect the copy.

The use of a BlockingCollection really simplifies producer-consumer relationships as compared to callbacks or semaphores, so if possible I would like to keep using them when exchanging data between processes.

(The semantics of a BlockingCollection are:
producers can simply add new items to the collection without worrying about additional locks or synchronization.
Consumers can simply iterate over the collection, when there are no new items the iterator blocks, whenever a new item is produced, one consumer is automatically woken up and unblocked)

Both processes will run on the same machine. One of the goals of separating producer and consumer into different processes was to allow the consumer to be restarted or modified while the producer is running. So ideally I am looking for a solution where the producer does not have to be aware of the consumer.

HugoRune
  • 13,157
  • 7
  • 69
  • 144
  • If one process is the producer and the other is the consumer, just send the new items from the producer to the consumer process using IPC. Have the `BlockingCollection` on the consumer side, and add new items on the consumer side as they come in. – Lucas Trzesniewski Nov 19 '14 at 17:49
  • The producer in this case is not necessarily aware of the consumer, i.e. the consumer may not be running the whole time, or may be restarted or replaced. (That is one of the purposes of separating the two in this case). I suppose the consumer could register callbacks with the producer, or listen to semaphores set by the producer, but I would prefer the elegance of the producer simply sharing a BlockingCollection with the consumer. I suspect it won't be that easy though. – HugoRune Nov 19 '14 at 17:54
  • `if I where to pass a BlockingCollection from one process to the other` No, just pass the data (using serialization/deserialization ) – L.B Nov 19 '14 at 18:12

1 Answers1

0

Based on the description of what is your requirement (i.e. your producer adds items and the consumer is sleeping until an item is added), I would suggest using MSMQ.

One process that adds messages to the queue, the other that is waiting for new messages to be available. The waiting thread would be blocked until a message is available (no polling used).

ken2k
  • 48,145
  • 10
  • 116
  • 176