AsyncSubject<T>。Subscribe メソッド

オブザーバーをサブジェクトにサブスクライブします。

Namespace:System.Reactive.Subjects
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'Declaration
Public Function Subscribe ( _
    observer As IObserver(Of T) _
) As IDisposable
'Usage
Dim instance As AsyncSubject
Dim observer As IObserver(Of T)
Dim returnValue As IDisposable

returnValue = instance.Subscribe(observer)
public IDisposable Subscribe(
    IObserver<T> observer
)
public:
virtual IDisposable^ Subscribe(
    IObserver<T>^ observer
) sealed
abstract Subscribe : 
        observer:IObserver<'T> -> IDisposable 
override Subscribe : 
        observer:IObserver<'T> -> IDisposable 
public final function Subscribe(
    observer : IObserver<T>
) : IDisposable

パラメーター

  • オブザーバー
    種類: System.IObserver<T>
    件名をサブスクライブするオブザーバー。

戻り値

種類: System.IDisposable
サブジェクトからオブザーバーの登録を解除するために使用できる IDisposable オブジェクト。

実装

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

解説

AsyncSubject の Subscribe メソッドは、AsyncSubject の監視可能なシーケンスにオブザーバー サブスクリプションを追加するために使用されます。 AsyncSubject は、IObserver がサブスクリプションを完了する OnComplete 呼び出しを受信した後にのみ、その IObservable インターフェイスに発行します。 これが発生すると、AsyncSubject に対する新しいサブスクリプションでも、そのサブスクリプションに最終結果が発行されます。

この例では、AsyncSubject を使用して、Range 演算子で生成された整数シーケンスをサブスクライブします。 AsyncSubject は、サブスクライブされているシーケンスが完了した場合にのみ値を返します。 シーケンスが完了すると、AsyncSubject によってシーケンスの最後の項目が発行されます。 AsyncSubject は、最終的な項目をキャッシュします。 その AsyncSubject に対する新しいサブスクリプションにも、そのサブスクリプションに発行された最終アイテムが含まれます。

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();
    }
  }
}

コード例によって次の出力が生成されました。

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

参照

リファレンス

AsyncSubject<T> クラス

System.Reactive.Subjects 名前空間