Subscription life cycle
What will happen if we want to stop a single observer from receiving messages from the observable
event source? If we change the program Main
from the preceding example to the following one, we could experience a wrong observer
life cycle design. Here's the code:
//this is the message observable responsible of producing messages using (var observer = new ConsoleIntegerProducer()) //those are the message observer that consume messages using (var consumer1 = observer.Subscribe(new IntegerConsumer(2))) using (var consumer2 = observer.Subscribe(new IntegerConsumer(3))) { using (var consumer3 = observer.Subscribe(new IntegerConsumer(5))) { //internal lifecycle } observer.Wait(); } Console.WriteLine("END"); Console.ReadLine();
Here is the result in the output console:
By using the using
construct
method, we should stop the life cycle of the consumer object. However...