Share a BlockingCollection across processes

335 views Asked by At

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.

1

There are 1 answers

0
ken2k On

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).