Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AsyncSubject<T>.Subscribe Method

Subscribes an observer to the subject.

Namespace:  System.Reactive.Subjects
Assembly:  System.Reactive (in System.Reactive.dll)
public IDisposable Subscribe(
	IObserver<T> observer
)

Parameters

observer
Type: System.IObserver<T>
Observer to subscribe to the subject.

Return Value

Type: System.IDisposable
IDisposable object that can be used to unsubscribe the observer from the subject.

Implements

IObservable<T>.Subscribe(IObserver<T>)

The Subscribe method of an AsyncSubject is used to add observer subscriptions to the AsyncSubject’s observable sequence. An AsyncSubject only publishes to its IObservable interface after its IObserver has received an OnComplete call completing its subscription. Once this has occurred any new subscriptions against the AsyncSubject will also have the final result published to that subscription.

In this example an AsyncSubject is used to subscribe to an integer sequence generated with the Range operator. An AsyncSubject only returns a value when the sequence it is subscribed to completes. Once the sequence has completed, the AsyncSubject will publish the final item in the sequence. The AsyncSubject caches the final item. Any new subscriptions against that AsyncSubject will also have the final item published to that subscription as well.

using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*******************************************************************************************************//
      //*** A subject acts similar to a proxy in that it acts as both a subscriber and a publisher          ***//
      //*** It's IObserver interface can be used to subscribe to multiple streams or sequences of data.     ***//
      //*** The data is then published through it's IObservable interface.                                  ***//
      //***                                                                                                 ***//
      //*** In this example an AsyncSubject is used to subscribe to an integer sequence from the Range      ***//
      //*** operator. An AsyncSubject only returns a value when the sequence it is subscribed to completes. ***//
      //*** Once the sequence has completed, the AsyncSubject will publish the final item in the sequence.  ***//
      //*** The AsyncSubject caches the final item. Any new subscriptions against that AsyncSubject will    ***//
      //*** also have the final item published to that subscription as well.                                ***//
      //*******************************************************************************************************//

      var intSequence = Observable.Range(0, 10, Scheduler.ThreadPool);

      AsyncSubject<int> myAsyncSubject = new AsyncSubject<int>();
      intSequence.Subscribe(myAsyncSubject);

      Thread.Sleep(1000);
      myAsyncSubject.Subscribe(i => Console.WriteLine("Final integer for subscription #1 is {0}\n", i),
                               () => Console.WriteLine("subscription #1 completed.\n"));
                                
      
      Console.WriteLine("Sleeping for 5 seconds before subscription2\n");
      Thread.Sleep(5000);

      myAsyncSubject.Subscribe(i => Console.WriteLine("Final integer for subscription #2 after 5 seconds is {0}\n", i),
                               () => Console.WriteLine("subscription #2 completed.\n"));


      Console.WriteLine("Press ENTER to exit...");
      Console.ReadLine();

      myAsyncSubject.Dispose();
    }
  }
}

The following output was generated by the example code.

Final integer for subscription #1 is 9

subscription #1 completed.

Sleeping for 5 seconds before subscription2

Final integer for subscription #2 after 5 seconds is 9

subscription #2 completed.

Press ENTER to exit...

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.