为使用者订阅观察者。
Namespace:System.Reactive.Subjects
装配: System.Reactive.dll) 中的 System.Reactive (
语法
'Declaration
Public Function Subscribe ( _
observer As IObserver(Of T) _
) As IDisposable
'Usage
Dim instance As BehaviorSubject
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
parameters
- 观测 器
类型: System.IObserver<T>
要订阅主题的观察者。
返回值
类型: System.IDisposable
可用于取消对使用者的观察者的订阅的 IDisposable 对象。
实现
IObservable<T>.订阅 (IObserver<T>)
备注
BehaviorSubject 缓冲它通过其 IObservable 接口发布的最后一项。 如果尚未通过其 IObservable 接口发布任何项,则构造函数中提供的初始项是当前缓冲的项。 订阅 BehaviorSubject 的 IObservable 接口时,发布的序列从当前缓冲的项开始。
一旦 BehaviorSubject 的 IObserver 接口收到完成,则不会缓冲或发布任何项。
示例
此示例演示 BehaviorSubject。 该示例使用 Interval 运算符每秒向整数序列发布一个整数。 发布 10 个整数后,Take 运算符将完成序列。 这是 BehaviorSubject 订阅的序列。
为 BehaviorSubject 的 IObservable 接口创建两个订阅,以显示它如何发布数据。
订阅 #1 :此订阅将从头开始,并显示序列中构造函数 (-9) 的初始缓冲值。
订阅 #2 :此订阅将在睡眠 5 秒后启动。 此订阅显示序列以当前缓冲的项开头。
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. ***//
//*** ***//
//*** A BehaviorSubject buffers the last item it published through its IObservable interface. If no ***//
//*** item has been published through its IObservable interface then the initial item provided in the ***//
//*** constructor is the current buffered item. When a subscription is made to the BehaviorSubject's ***//
//*** IObservable interface, the sequence published begins with the currently buffered item. ***//
//*** ***//
//*** No items are buffered or published from a BehaviorSubject once its IObserver interface receives ***//
//*** a completion. ***//
//*** ***//
//*** In this example, we use the Interval operator to publish an integer to a integer sequence every ***//
//*** second. The sequence will be completed by the Take operator after 10 integers are published. ***//
//*** This will be the sequence that the BehaviorSubject subscribes to. ***//
//*** ***//
//*** We will create 2 subscriptions to the BehaviorSubject's IObservable interface to show how it ***//
//*** publishes it's data. ***//
//*** ***//
//*** Subscription #1 : This subscription will start at the very beginning and will show the initial ***//
//*** buffered value from the constructor (-9) in the sequence. ***//
//*** ***//
//*** Subscription #2 : This subscription will start after a 5 sec. sleep showing the sequence starts ***//
//*** with the currently buffered item. ***//
//********************************************************************************************************//
BehaviorSubject<long> myBehaviorSubject = new BehaviorSubject<long>((-9));
Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.ThreadPool).Take(10).Subscribe(myBehaviorSubject);
//********************************************************************************************************//
//*** Subscription #1 : This subscription will start at the very beginning and will show the initial ***//
//*** buffered value from the constructor (-9) in the sequence. ***//
//********************************************************************************************************//
EventWaitHandle wait1 = new EventWaitHandle(false, EventResetMode.ManualReset);
myBehaviorSubject.Subscribe(x => Console.WriteLine("Subscription #1 observes : " + x),
() =>
{
Console.WriteLine("Subscription #1 completed.");
wait1.Set();
});
//********************************************************************************************************//
//*** Subscription #2 : This subscription will start after a 5 sec. sleep showing the sequence starts ***//
//*** with the currently buffered item. ***//
//********************************************************************************************************//
Thread.Sleep(5000);
EventWaitHandle wait2 = new EventWaitHandle(false, EventResetMode.ManualReset);
myBehaviorSubject.Subscribe(x => Console.WriteLine("{0,30}Subscription #2 observes : {1}", " ", x),
() =>
{
Console.WriteLine("{0,30}Subscription #2 completed.", " ");
wait2.Set();
});
//**************************************************//
// *** Wait for completion on both subscriptions ***//
//**************************************************//
WaitHandle.WaitAll(new WaitHandle[] { wait1, wait2 });
myBehaviorSubject.Dispose();
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
}
}
示例代码的以下输出显示了重叠的订阅。
Subscription #1 observes : -9
Subscription #1 observes : 0
Subscription #1 observes : 1
Subscription #1 observes : 2
Subscription #1 observes : 3
Subscription #1 observes : 4
Subscription #2 observes : 4
Subscription #1 observes : 5
Subscription #2 observes : 5
Subscription #1 observes : 6
Subscription #2 observes : 6
Subscription #1 observes : 7
Subscription #2 observes : 7
Subscription #1 observes : 8
Subscription #2 observes : 8
Subscription #1 observes : 9
Subscription #2 observes : 9
Subscription #1 completed.
Subscription #2 completed.
Press ENTER to exit...