AsyncSubject < T > .Subscribe 方法
訂閱主體的觀察者。
Namespace:System.Reactive.Subjects
裝配: System.Reactive.dll) 中的 System.Reactive (
語法
'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 > .訂閱 (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...