Skip to content

)} ,System.Boolean)} ,System.Object)} ,System.Reactive.Concurrency.AsyncLock)}

Observer.Synchronize(IObserver) method

Defined in

Type: Observer Namespace: System.Reactive Assembly: System.Reactive.dll

Applies to

netstandard2.0

Overloads

  • 1. public static IObserver<T> Synchronize<T>(IObserver<T> observer)
  • 2. public static IObserver<T> Synchronize<T>(IObserver<T> observer, bool preventReentrancy)
  • 3. public static IObserver<T> Synchronize<T>(IObserver<T> observer, object gate)
  • 4. public static IObserver<T> Synchronize<T>(IObserver<T> observer, AsyncLock asyncLock)

1. Overload

public static IObserver<T> Synchronize<T>(IObserver<T> observer)

Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently from multiple threads. This overload is useful when coordinating access to an observer. Notice reentrant observer callbacks on the same thread are still possible.

Type parameters

NameDescription
TThe type of the elements received by the source observer.

Parameters

NameTypeDescription
observerIObserverThe observer whose callbacks should be synchronized.

Returns: IObserver -- An observer that delivers callbacks to the specified observer in a synchronized manner.

Remarks

Because a Monitor is used to perform the synchronization, there's no protection against reentrancy from the same thread. Hence, overlapped observer callbacks are still possible, which is invalid behavior according to the observer grammar. In order to protect against this behavior as well, use the Synchronize overload, passing true for the second parameter.

Exceptions

TypeCondition
System.ArgumentNullExceptionobserver is null.

2. Overload

public static IObserver<T> Synchronize<T>(IObserver<T> observer, bool preventReentrancy)

Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently. This overload is useful when coordinating access to an observer. The preventReentrancy parameter configures the type of lock used for synchronization.

Type parameters

NameDescription
TThe type of the elements received by the source observer.

Parameters

NameTypeDescription
observerIObserverThe observer whose callbacks should be synchronized.
preventReentrancyboolIf set to true, reentrant observer callbacks will be queued up and get delivered to the observer in a sequential manner.

Returns: IObserver -- An observer that delivers callbacks to the specified observer in a synchronized manner.

Remarks

When the preventReentrancy parameter is set to false, behavior is identical to the Synchronize overload which uses a Monitor for synchronization. When the preventReentrancy parameter is set to true, an AsyncLock is used to queue up callbacks to the specified observer if a reentrant call is made.

Exceptions

TypeCondition
System.ArgumentNullExceptionobserver is null.

3. Overload

public static IObserver<T> Synchronize<T>(IObserver<T> observer, object gate)

Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently by multiple threads, using the specified gate object for use by a Monitor-based lock. This overload is useful when coordinating multiple observers that access shared state by synchronizing on a common gate object. Notice reentrant observer callbacks on the same thread are still possible.

Type parameters

NameDescription
TThe type of the elements received by the source observer.

Parameters

NameTypeDescription
observerIObserverThe observer whose callbacks should be synchronized.
gateobjectGate object to synchronize each observer call on.

Returns: IObserver -- An observer that delivers callbacks to the specified observer in a synchronized manner.

Remarks

Because a Monitor is used to perform the synchronization, there's no protection against reentrancy from the same thread. Hence, overlapped observer callbacks are still possible, which is invalid behavior according to the observer grammar. In order to protect against this behavior as well, use the Synchronize overload.

Exceptions

TypeCondition
System.ArgumentNullExceptionobserver or gate is null.

4. Overload

public static IObserver<T> Synchronize<T>(IObserver<T> observer, AsyncLock asyncLock)

Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently, using the specified asynchronous lock to protect against concurrent and reentrant access. This overload is useful when coordinating multiple observers that access shared state by synchronizing on a common asynchronous lock.

Type parameters

NameDescription
TThe type of the elements received by the source observer.

Parameters

NameTypeDescription
observerIObserverThe observer whose callbacks should be synchronized.
asyncLock[AsyncLock](#Gate object to synchronize each observer call on.

Returns: IObserver -- An observer that delivers callbacks to the specified observer in a synchronized manner.

Exceptions

TypeCondition
System.ArgumentNullExceptionobserver or asyncLock is null.