Export (0) Print
Expand All

IObservable(Of T).Subscribe Method

Notifies the provider that an observer is to receive notifications.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Function Subscribe ( _
	observer As IObserver(Of T) _
) As IDisposable

Parameters

observer
Type: System.IObserver(Of T)

The object that is to receive notifications.

Return Value

Type: System.IDisposable
A reference to an interface that allows observers to stop receiving notifications before the provider has finished sending them.

The Subscribe method must be called to register an observer for push-based notifications. A typical implementation of the Subscribe method does the following:

  • It stores a reference to the observer in a collection object, such as a List(Of T) object.

  • It returns a reference to an IDisposable interface. This enables observers to unsubscribe (that is, to stop receiving notifications) before the provider has finished sending them and called the subscriber's OnCompleted method.

At any given time, a particular instance of an IObservable(Of T) implementation is responsible for handling all subscriptions and notifying all subscribers. Unless the documentation for a particular IObservable(Of T) implementation indicates otherwise, observers should make no assumptions about the IObservable(Of T) implementation, such as the order of notifications that multiple observers will receive.

The following example illustrates the Subscribe method for an application that reports latitude and longitude information. It defines an IList(Of T) collection object that stores references to all observers. It also returns a private class named Unsubscriber that implements the IDisposable interface and enables subscribers to stop receiving event notifications. See the Example section of the IObservable(Of T) topic for the complete example.

Private observers As List(Of IObserver(Of Location))

Public Function Subscribe(ByVal observer As System.IObserver(Of Location)) As System.IDisposable _
                         Implements System.IObservable(Of Location).Subscribe
   If Not observers.Contains(observer) Then
      observers.Add(observer)
   End If 
   Return New Unsubscriber(observers, observer)
End Function 

Private Class Unsubscriber : Implements IDisposable
   Private _observers As List(Of IObserver(Of Location))
   Private _observer As IObserver(Of Location)

   Public Sub New(ByVal observers As List(Of IObserver(Of Location)), ByVal observer As IObserver(Of Location))
      Me._observers = observers
      Me._observer = observer
   End Sub 

   Public Sub Dispose() Implements IDisposable.Dispose
      If _observer IsNot Nothing AndAlso _observers.Contains(_observer) Then
         _observers.Remove(_observer)
      End If 
   End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft