将观察者订阅主题。
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
parameters
- 观测 器
类型: 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...